Nullable<> types are a BCL, CLR, or both implem

2019-06-26 11:11发布

问题:

Some time ago I thought that Nullable<> value types are classes, encapsulating value types and a bool to HasValue. With some implicit cast operador for null, just implemented at BCL.

But being a struct, how this can be achieved? Nullable<> struct is "special" for CLR?

回答1:

Nullable<T> is defined as a normal struct, but there's special hooks within the CLR to box/unbox an instance of [mscorlib]System.Nullable`1 to null according to the HasValue property. There's more details on this here



回答2:

Here's the MSDN article on Nullable Types.

http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=VS.100).aspx

I'm not sure what you're trying to get at with Nullable<>, unless you're mis-understanding that Nullable types are instances of the System.Nullable<T> struct.



回答3:

Nullable<> is a struct implemented within mscorlib.

One special thing is in the C# compiler that recognizes X? as an alias for Nullable<X>.