What was the design rationale for making void not a primitive type?

后端 未结 3 1436
情书的邮戳
情书的邮戳 2021-02-05 02:17

When using reflection Type.IsPrimitive on a void type returns false.Coming from a C++ background this was surprising.

Looking at the C# 6.0 spe

3条回答
  •  独厮守ぢ
    2021-02-05 02:51

    From reading the C# Specification, there is no mention of a type void. It is written about as a return type. In the table of contents, it is not categorised under reference or value types. It is mentioned as a keyword.

    The section on the typeof function says:

    The third form of typeof-expression consists of a typeof keyword followed by a parenthesized void keyword. The result of an expression of this form is the System.Type object that represents the absence of a type. The type object returned by typeof(void) is distinct from the type object returned for any type. This special type object is useful in class libraries that allow reflection onto methods in the language, where those methods wish to have a way to represent the return type of any method, including void methods, with an instance of System.Type.

    Which to me indicates that void has an associated type but is only used in the case where you want reflection to give you something tangible because of the type system. Otherwise void is just a keyword.

提交回复
热议问题