I want to do something like this: default(typeof(MyClass.MyProperty))
so that in the event that the type of MyProperty
needs to change, my expression default(typeof(MyClass.MyProperty))
remains correct. I think TypeScript has infiltrated my brain, and I'm not sure this is possible in C#, at least not as simply as it is in TypeScript. But, for example, if MyProperty
is of one type, like int
, and for whatever reason, it changes to string
later, I'd want my default
expression to remain true. Is this overkill? Should I just use default(int)
and make a comment that hopefully someone later catches in the event they are changing the type of that property?
From the docs:
typeof
The argument to thetypeof
operator must be the name of a type or a type parameter
So exactly that is not possible.
Note that you don't need to specify type, you can use the default
literal:
MyClass mc = ...;
mc.MyProperty = default;