How can you create a default – non indexer – property in C#?
What I mean by this is I can see that I can create indexer default properties as illustrated on this MSDN page.
This allows me to do things like
Widgets widgets = new Widgets();
Widget result = widgets[1];
But what if I want to achieve something like what Nullable<T> does?
Where you can take
Nullable<decimal> nullDec = 1.23m;
decimal result = nullDec.Value;
OR
decimal result = (decimal)nullDec;
Which I assume is simply a default property implementation to nullDec.Value???
Nullable<T>has special handling in the compiler, but you can do most of that by adding implicit or explicit static conversion operators.For example, for type
Fooyou can add an operator:allowing:
and