I was thinking the other day about the difference between values types and reference types, and how they can have a very profound impact on the performance (and even stability) of a program if you do not watch out. Their behavior is different (however subtle) enough such that some wierd bugs could creep into your program. I realized that with the generic class functionality coming in .NET 2.0, I could create a reference type wrapper around a value type. Easy enough ...
public class Reference<T>
{
private T rootValue;
public T Value
{
get {return rootValue;}
set {rootValue = value;}
}
public Reference(T argValue)
{
rootValue = argValue;
}
}
...
Reference<int> myInt = new Reference(56);
Console.WriteLine(myInt.Value);
now you can pass myInt around. Since it's a reference type it will not create a new copy of "56" every time it's passed into a function or something. This might not be so useful for something as small as an int, but what if you had a struct (value type) that held a large amount of data; for example a large byte array or something. The benefits of passing around a reference to that data becomes immediately clear.
Of course, one could argue that you could just use the pre-existing facilities for achieving this ... that is to use the ref keyword. Honestly, that would probably be a better design. This idea just popped into my head and I figured I'd write about it just because it can be done :-)