I know that it is possible to have Nullable value types that wraps the value type and gives ability to store null. But is there a technical reason do not allow the value type to be null or the reason is only conceptual?
我知道有可能有可空值类型来包装值类型并提供存储空值的能力。但是,是否有技术原因不允许值类型为null,或者原因仅仅是概念性的?
2 个解决方案
#1
33
A reference type is storeed as a reference (like a pointer) to an object instance.null
means a reference that isn't pointing to an instance of an object.
引用类型存储为对象实例的引用(如指针)。null表示没有指向对象实例的引用。
Value types are stored as the values themselves, without any references.
Therefore, it doesn't make sense to have a null
value type—the value type by definition contains a value.
值类型被存储为值本身,没有任何引用。因此,让空值类型-值类型根据定义包含值是没有意义的。
Nullable<T>
is a value type with a HasValue
flag that can be false
to indicate that there is no value. It still has a value (when HasValue
is false
, Value
is default(T)
), but the HasValue
flag tells you to ignore the value.
It has nothing to do with null
, except that the CLR automatically unboxes null
boxed values to a Nullable<T>
with HasValue
set to false
.
Nullable
#2
0
A value type like 'Int32' is stored using thirty-two bits of storage. There are precisely 4,294,967,296 values that may be represented by 32 bits, and an Int32 can hold 4,294,967,296 different values. If -2,147,483,648 were not a valid Int32 value, it might be possible to use that to represent "null", but the fact that its binary representation isn't all zeroes would complicate things. By contrast, the number of possible bit combinations in a reference type far exceeds the number of possible valid references, so there's no difficulty reserving a bit combination to represent "null".
像'Int32'这样的值类型是使用32位存储来存储的。正好有4,294,967,296个值可以用32位表示,而Int32可以容纳4,294,96,296个不同的值。如果-2,147,483,648不是一个有效的Int32值,可能可以用它来表示“null”,但是它的二进制表示并不是都是0的事实会使事情变得复杂。相比之下,引用类型中可能的位组合的数量远远超过可能的有效引用的数量,因此保存一个位组合来表示“null”并不困难。
#1
33
A reference type is storeed as a reference (like a pointer) to an object instance.null
means a reference that isn't pointing to an instance of an object.
引用类型存储为对象实例的引用(如指针)。null表示没有指向对象实例的引用。
Value types are stored as the values themselves, without any references.
Therefore, it doesn't make sense to have a null
value type—the value type by definition contains a value.
值类型被存储为值本身,没有任何引用。因此,让空值类型-值类型根据定义包含值是没有意义的。
Nullable<T>
is a value type with a HasValue
flag that can be false
to indicate that there is no value. It still has a value (when HasValue
is false
, Value
is default(T)
), but the HasValue
flag tells you to ignore the value.
It has nothing to do with null
, except that the CLR automatically unboxes null
boxed values to a Nullable<T>
with HasValue
set to false
.
Nullable
#2
0
A value type like 'Int32' is stored using thirty-two bits of storage. There are precisely 4,294,967,296 values that may be represented by 32 bits, and an Int32 can hold 4,294,967,296 different values. If -2,147,483,648 were not a valid Int32 value, it might be possible to use that to represent "null", but the fact that its binary representation isn't all zeroes would complicate things. By contrast, the number of possible bit combinations in a reference type far exceeds the number of possible valid references, so there's no difficulty reserving a bit combination to represent "null".
像'Int32'这样的值类型是使用32位存储来存储的。正好有4,294,967,296个值可以用32位表示,而Int32可以容纳4,294,96,296个不同的值。如果-2,147,483,648不是一个有效的Int32值,可能可以用它来表示“null”,但是它的二进制表示并不是都是0的事实会使事情变得复杂。相比之下,引用类型中可能的位组合的数量远远超过可能的有效引用的数量,因此保存一个位组合来表示“null”并不困难。