Box<T> Class

Definition

A class that represents a boxed T value on the managed heap. This is a "shadow" type that can be used in place of a non-generic Object reference to a boxed value type, to make the code more expressive and reduce the chances of errors. Consider this example:

object obj = 42;

// Manual, error prone unboxing
int sum = (int)obj + 1;

In this example, it is not possible to know in advance what type is actually being boxed in a given Object instance, making the code less robust at build time. The Box<T> type can be used as a drop-in replacement in this case, like so:

Box<int> box = 42;

// Build-time validation, automatic unboxing
int sum = box.Value + 1;

This type can also be useful when dealing with large custom value types that are also boxed, as it allows to retrieve a mutable reference to the boxing value. This means that a given boxed value can be mutated in-place, instead of having to allocate a new updated boxed instance.

public sealed class Box<T> where T : struct
type Box<'T (requires 'T : struct)> = class
Public NotInheritable Class Box(Of T)

Type Parameters

T

The type of value being boxed.

Inheritance
Box<T>

Methods

DangerousGetFrom(Object)

Returns a Box<T> reference from the input Object instance.

Equals(Object)

Determines whether the specified object is equal to the current object.

GetFrom(Object)

Returns a Box<T> reference from the input Object instance.

GetHashCode()

Serves as the default hash function.

ToString()

Returns a string that represents the current object.

TryGetFrom(Object, Box<T>)

Tries to get a Box<T> reference from an input Object representing a boxed T value.

Operators

Implicit(Box<T> to T)

Implicitly gets the T value from a given Box<T> instance.

Implicit(T to Box<T>)

Extension Methods

GetReference<T>(Box<T>)

Gets a T reference from a Box<T> instance.

Applies to