lets say I have the following
public class A
{
private string _someField;
public string SomeField { get { return _someField; } }
}
For some reason I am checking the default of this class and I would like to set the default for a class, just like a default of type int is 0, I would like in the above class for my default of Somefield to be “hello”;
int i = default(int); // i is 0
A myClass = default(A);
string s = myClass.SomeField; // s is hello
This is more just for my own theoretical satisfaction rather than practical application. Just wondering.
There is no way of overloading default(T).
To me, it really sounds like you’re asking for non-nullable reference types which don’t yet exist in .NET. Have a look here for an implementation: http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx