c# - 空文字的类型是什么?

标签 c# .net language-lawyer

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 specificationC# 2.0 language specification .

关于c# - 空文字的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204578/

相关文章:

c# - 如何将 viewModel 绑定(bind)到 View

c# - 我如何将 .net 的 HttpWebRequest 移植到 iOs 的 URLRequest

c# - 将 XML 设置为 XElement 的值

c++ - 我应该如何在恒定长度的 STL 样式数据结构中实现 max_size?

c++ - 开关盒中的变量 : UB or compiler bug?

c++ - 不完整类型的嵌套静态 constexpr。 C++ 是否有效?

c# - 如何使用Dockerfile覆盖ASP.NET Core连接字符串

c# - Command Line.Parser 是否支持选项多次出现?

c# - Animation Completed 稍微到很快就会触发

.net - 拉伸(stretch)控件以填充 ItemsControl