加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:48:46 所属栏目:百科 来源:网络整理
导读: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 ///
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,但不完全集成到类型系统中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读