Is it right to cast null to nullable when using ternary expression assigning to a nullable type?

It feels strange to me to be casting null to a type so I wanted to double check that this is the right way to do this:

decimal? d = data.isSpecified ? data.Value : (decimal?)null;

alt text

alt text

NOTE: I am marking the answer that suggests the method that I personally like the best:

decimal? d = data.isSpecified ? data.Value : default(decimal?)
12
задан Aaron Anodide 29 October 2010 в 16:33
поделиться