Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Boxing a value type in .NET to make it a reference type

0.00/5 (No votes)
30 Sep 2011 1  
How to wrap a value type so it can be used in positions where a reference type is required
Sometimes, you want to pass or store data which is in a value type in a context where only a reference type makes sense. This is either because you want to be able to modify the value from another location (as in this question[^]), or because you want to pass it as a type parameter to a generic class or method which has a where T: class (or where T: new()) restriction.

You can write a boxing class which can be used in such scenarios:
public class Box<T> where T:struct {
 public T Value { get; set; }

 public Box() : this(default(T)) {} // so it can be created from new()
 public Box(T value) { this.Value = value; }
 
 public static implicit operator T(Box<T> box) { return box.Value; }
 public static implicit operator Box<T>(T value) { return new Box<T>(value); }
}


Usage: If you want to use it as a 'pointer', you need to store the reference and pass it explicitly:
Box<int> refint = 5;
ModifyingClass instance = new ModifyingClass(refint);
instance.Adjust();
Debug.Assert(6 == refint);

// where
class ModifyingClass {
 Box<int> target;
 public ModifyingClass(Box<int> i) { target = i; }
 public void Adjust() { target.Value++; }
}


To pass it as a prototype to a generic class, simply do
SomeRestrictedGeneric<Box<T>> variable = new SomeRestrictedGeneric<Box<T>>()

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here