D 大家好,我想知道 C# 中 null
文字的类型是什么?
在 Java 中,null
字面量 is of the special null type :
There is also a special null type, the type of the expression
null
, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type.
在C++11中,有nullptr
(老哥们NULL
的推荐版本),即is of type std::nullptr_t
.
我在 MSDN 上搜索了有关 C# 的信息,但是 specification似乎对此什么也没说。
最佳答案
根据ECMA C# language specification :
9.4.4.6 空文字:
The type of a null-literal is the null type (§11.2.7).
11.2.7 空类型:
The null literal (§9.4.4.6) evaluates to the null value, which is used to denote a reference not pointing at any object or array, or the absence of a value. The null type has a single value, which is the null value. Hence an expression whose type is the null type can evaluate only to the null value. There is no way to explicitly write the null type and, therefore, no way to use it in a declared type. Moreover, the null type can never be the type inferred for a type parameter (§25.6.4)
所以要回答你的问题,null 是它自己的类型 - null 类型。
尽管很奇怪 C# 4.0 language specification 中没有提到它或 C# 3.0 language specification但在 overview of C# 3.0 中提到, ECMA C# language specification和 C# 2.0 language specification .
关于c# - 空文字的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204578/