Nullable<> types are a BCL, CLR, or both implementation?

我怕爱的太早我们不能终老 提交于 2019-12-10 17:55:49

问题


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>.



来源:https://stackoverflow.com/questions/4694264/nullable-types-are-a-bcl-clr-or-both-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!