What is the type of null literal?

前端 未结 3 1362
礼貌的吻别
礼貌的吻别 2020-11-30 00:14

Dear all, I wonder what is the type of null literal in C#?

In Java, the null literal is of the special null type:

Th

3条回答
  •  臣服心动
    2020-11-30 00:58

    According to the ECMA C# language specification:

    9.4.4.6 The null literal:

    The type of a null-literal is the null type (§11.2.7).

    11.2.7 The null type:

    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)

    So to answer your question, null is it's own type - the null type.

    Although it's odd how it's not mentioned in the C# 4.0 language specification or the C# 3.0 language specification but is mentioned in the overview of C# 3.0, the ECMA C# language specification and the C# 2.0 language specification.

提交回复
热议问题