I always thought that the nullable types of the .NET framework where nothing but a nice construct that the framework gave us, but wasn’t anything new added to the language itself.
That is, until today, for purposes of a demonstration, I tried to create my own Nullable struct.
I get everything I need, except the capability of doing this:
var myInt = new Nullable<int>(1);
myInt = 1;
The problem is the second statement, as I cannot, by any means, overload the assignment operator.
My question is: Is this a special case in the language where the assignment operator has an overload? If not, how would you approach making that previous example work?
Thanks!
The assignment is question is handled using an implicit operator declared as:
public static implicit operator Nullable<T> (T value)This is handled without any language-specific features.
The main change to the langauge for nullable support is the ability to write this as:
You could implement this in your type via:
That being sad, there is a lot of features related to
Nullable<T>the C# language and CLR does that can’t be duplicated in your code.