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

后端 未结 3 1438
情书的邮戳
情书的邮戳 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:58

    Why isn't void a primitive type? Because it isn't something you can instantiate. It isn't a primitive type, nor a reference type. It is nothing at all.

    Eric Lippert describes some 'problems' with the void type in this post on Software Engineering, which goes into the specifics of void as a type to use in delegates and Actions:

    A type system is essentially a system for making logical deductions about what operations are valid on particular values; a void returning method doesn't return a value, so the question "what operations are valid on this thing?" don't make any sense at all. There's no "thing" for there to be an operation on, valid or invalid.

    Making it a primitive type defeats the special meaning and purpose of void in the VES (Virtual Execution System), as Eric explains later on:

    The effect of a call to a void method is fundamentally different than the effect of a call to a non-void method; a non-void method always puts something on the stack, which might need to be popped off. A void method never puts something on the stack.

    Making void a primitive type breaks this rule, although you could argue its usefulness, as Eric explains further in the post referenced.

提交回复
热议问题