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
{
}
}
最佳答案
System.Void
实际上是一种标记类型 - 成员如 MethodInfo.ReturnType
必须有 一些 方式来表示 void
,System.Void
是 .NET 团队选择这样做的方式。
你不应该像普通类型那样使用它。这是一种有效的解决方法 - 有点像 F# Unit
type ,但没有完全集成到类型系统中。
关于c# - 为什么在 .NET 中将返回类型 void 声明为 struct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574862/