Click here to Skip to main content
11,492,385 members (63,028 online)
Click here to Skip to main content

Tagged as

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

, 30 Sep 2011 CPOL 6.3K 2
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

BobJanova

United Kingdom United Kingdom
I'm a recent graduate (MSci) from the University of Cambridge, no longer studying Geology. Programming is a hobby so I get to write all the cool things and not all the boring things Smile | :) . However I now have a job in which I have to do a bit of work with a computer too.

Comments and Discussions

 
GeneralWhat is the advantage of your solution in opposite to just m... Pin
Klaus Luedenscheidt30-Sep-11 19:28
memberKlaus Luedenscheidt30-Sep-11 19:28 
GeneralRe: You can't save a reference passed as a ref parameter for lat... Pin
BobJanova1-Oct-11 1:39
memberBobJanova1-Oct-11 1:39 
GeneralReason for my vote of 5 Excellent explanation from the stand... Pin
SAKryukov29-Sep-11 20:22
mvpSAKryukov29-Sep-11 20:22 
GeneralRe: Some poorly written generic containers have a class or new()... Pin
BobJanova30-Sep-11 3:34
memberBobJanova30-Sep-11 3:34 
Some poorly written generic containers have a class or new() constraint. We had one in our application which initially made me research this, and although we are allowed to change that code (the constraint was not necessary once you know about default(T)) so primitive types were allowed, it is easy to imagine a situation where someone might not be able to.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 30 Sep 2011
Article Copyright 2011 by BobJanova
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid