简体   繁体   English

为什么返回类型void在.NET中声明为struct?

[英]Why is return type void declared as struct in .NET?

AFAIK void means nothing in terms of programming language. AFAIK void在编程语言方面没有任何意义。 So why in .Net framework it is declared as struct ? 那么为什么在.Net框架中它被声明为struct

using System.Runtime.InteropServices;

namespace System
{
  /// <summary>
  /// Specifies a return value type for a method that does not return a value.
  /// </summary>
  /// <filterpriority>2</filterpriority>
  [ComVisible(true)]
  [Serializable]
  [StructLayout(LayoutKind.Sequential, Size = 1)]
  public struct Void
  {
  }
}

System.Void is effectively a marker type - members like MethodInfo.ReturnType have to have some way of representing void , and System.Void is the way the .NET team chose to do so. System.Void实际上是一种标记类型 - 像MethodInfo.ReturnType这样的成员必须有一些表示void ,而System.Void是.NET团队选择这样做的方式。

You shouldn't use it like a normal type. 你不应该像普通类型一样使用它。 It's a workaround, effectively - a little like the F# Unit type , but not as fully integrated into the type system. 这是一种有效的解决方法 - 有点像F# Unit类型 ,但不完全集成到类型系统中。

A method descriptor contains a field for the method's return type. 方法描述符包含方法返回类型的字段。 While it would be possible to have that field be null for void functions, that would require that code which wants to eg report the method's return type say something like: 虽然可以使该字段对于void函数为null ,但这需要想要报告方法的返回类型的代码如下所示:

string theReturnType = theMethod.ReturnType ? theMethod.ReturnType.ToString() : "null";

rather than simply saying: 而不是简单地说:

string theReturnType = theMethod.ReturnType.ToString();

There are enough cases where code has to do something with the method's return type that having to special-case null in all of them would be a far greater bother than simply having a dummy type "null" which can be returned. 有足够多的情况下,代码必须对方法的返回类型做一些事情,在所有这些方法中必须使用特殊情况null才会比仅仅返回可以返回的虚拟类型“null”更麻烦。

Incidentally, although so far as I know void is the only type which is usable for a method return value but no other purpose, there are other cases where it would be useful to have a type which could be used for public return values, but not used in outside storage-location declarations, such as when writing a fluent interface (if outside code could be allowed to access members of biz.boz(3) , but could not store the value itself, then in a construct like biz.boz(3).foo(9).bar(2).build() , the foo method could know that it held the only reference anywhere in the universe to the object returned by boz(3) , and would thus be free to either mutate it or return a new instance, at its convenience). 顺便说一下,虽然我知道void是唯一可用于方法返回值但没有其他目的的类型,但在其他情况下,有一个类型可用于公共返回值是有用的,但不是用于外部存储位置声明,例如在编写流畅的界面时(如果允许外部代码访问biz.boz(3)成员,但无法存储值本身,则在biz.boz(3).foo(9).bar(2).build()biz.boz(3).foo(9).bar(2).build()foo方法可以知道它在宇宙中的任何地方都拥有boz(3)返回的对象的唯一引用,因此可以自由地进行变异它或在方便时返回一个新实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 .NET 在结构中使用引用类型? - Why .NET uses reference type inside struct? 为什么事件处理程序的返回类型总是 void? - Why do event handlers always have a return type of void? 为什么部分方法只能有void返回类型? - Why partial methods can only have void return type? 为什么条件方法的返回类型必须为void? - Why does conditional methods must have a return type of void? 多播代表的返回类型必须为void。 为什么? - Multicast Delegates must have a return type of void. Why? 为什么entityframework Remove&RemoveRange返回类型不是void或bool - Why entityframework Remove & RemoveRange return type not void or bool 为什么将返回类型添加到void返回方法会导致MissingMethodException - Why adding a return type to a void returning method causes a MissingMethodException “即使函数设置为void,“类,结构或接口方法也必须具有返回类型” - “Class, struct, or interface method must have a return type” Even when function is set to void 结构是一个值类型,那么当在结构体中声明字符串时,为什么编译器不给出任何错误 - Structure is an value type, Then why don't compiler gives any error when string is declared within struct 带参数和 void 返回的 Func 类型 - Func type with a parameter and void return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM