Box<T> Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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) | |
Equals(Object) |
Determines whether the specified object is equal to the current object. |
GetFrom(Object) | |
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 |
Operators
Implicit(Box<T> to T) |
Implicitly gets the |
Implicit(T to Box<T>) |
Extension Methods
GetReference<T>(Box<T>) |
Gets a |