How is null represented in .NET

前端 未结 4 1949
南方客
南方客 2021-01-18 08:20

I was having a conversation with a co-worker and the subject of null came up. He was telling me that in .NET behind the scenes it is just a really small number. I always tho

4条回答
  •  攒了一身酷
    2021-01-18 08:48

    When a nullable type instance is set to null, its underlying value is zero.

    To be more specific, a nullable type is a structure that combines a value of the underlying type together with a boolean null indicator. An instance of a nullable type has two public read-only properties: HasValue of type bool, and Value of the nullable type’s underlying type.

    HasValue is true for a non-null instance and false for a null instance.

    So when HasValue is true, the Value property returns the contained value. When HasValue is false, an attempt to access the Value property throws an exception. But if you could access it, you would find it contains zero.

提交回复
热议问题