Box
Forked from Community Toolkit.
A class representing a boxed value type, providing build-time validation and automatic unboxing.
Box
Methods
GetFrom
public static Box<T> GetFrom(object obj)
Returns a Box<T> reference from the input object instance, representing a boxed T value.
DangerousGetFrom
public static Box<T> DangerousGetFrom(object obj)
Returns a Box<T> reference from the input object instance, representing a boxed T value. This method doesn't check the actual type of obj, so it is the responsibility of the caller to ensure it actually represents a boxed T value and not some other instance.
TryGetFrom
public static bool TryGetFrom(object obj, out Box<T>? box)
Tries to get a Box<T> reference from an input object representing a boxed T value. Returns true if a Box<T> instance was retrieved correctly, false otherwise.
Operators
implicit operator T
public static implicit operator T(Box<T> box)
Implicitly gets the T value from a given Box<T> instance.
implicit operator Box
public static implicit operator Box<T>(T value)
Implicitly creates a new Box<T> instance from a given T value.
Usage
Box and Unbox Value Type with Build-time Validation
Box<int> box = 42;
int sum = box.Value + 1;
Retrieve a Mutable Reference to a Boxed Value
Box<MyStruct> box = new MyStruct { Field1 = 1, Field2 = 2 };
ref MyStruct myStructRef = ref box.GetReference();
myStructRef.Field1 = 3;
Extension Methods
GetReference
public static ref T GetReference<T>(this Box<T> box) where T : struct
Gets a T reference from a Box<T> instance.
Usage
Box<MyStruct> box = new MyStruct { Field1 = 1, Field2 = 2 };
ref MyStruct myStructRef = ref box.GetReference();