使void不是原始类型的设计原理是什么? [英] What was the design rationale for making void not a primitive type?

查看:70
本文介绍了使void不是原始类型的设计原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void类型上使用反射Type.IsPrimitive时返回false.从C ++背景来看,这令人惊讶.

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

查看 C#6.0规范(第82页)没有提到void类型,这可能意味着它没有被完全归类为类型.

Looking at the C# 6.0 spec (Page 82) does not mention the void type, which could mean that it isn't categorized as a type altogether.

语言规范中是否有任何内容将void归类为其他内容.或任何其他提及此背后原因的讨论?

Is there anything in the language spec or otherwise that categorizes void as something else. Or any other discussion or such which mentions the reason behind this?

推荐答案

为什么不使原始类型无效?因为它不是您可以实例化的东西.它不是原始类型,也不是引用类型.没什么.

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在有关软件工程的帖子中介绍了一些无效类型的问题", void作为要在委托和Action s中使用的类型的详细信息:

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.

将其制成原始类型会破坏VES(虚拟执行系统)中void的特殊含义和目的,正如Eric稍后解释的那样:

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

调用void方法的效果与调用非void方法的效果从根本上是不同的.非无效方法总是将某些东西放到堆栈上,这可能需要弹出. void方法永远不会在堆栈上放任何东西.

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.

使void为基本类型会破坏此规则,尽管您可能会争辩它的用处,正如Eric在所引用的文章中进一步解释的那样.

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

这篇关于使void不是原始类型的设计原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆