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

PaR*_*RaJ 11 .net c# vb.net clr

AFAIK void在编程语言方面毫无意义.那么为什么在.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
  {
  }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 16

System.Void是一个有效的标记类型-成员一样MethodInfo.ReturnType必须有一些代表的方式void,并且System.Void是.NET团队选择了这样做的方式.

你不应该像普通类型一样使用它.这是一种有效的解决方法 - 有点像F#Unit类型,但不完全集成到类型系统中.