为什么无参数的Guid构造产生一个空的GUID? [英] Why does the parameterless Guid constructor generate an empty GUID?

查看:264
本文介绍了为什么无参数的Guid构造产生一个空的GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为何无参数的Guid构造函数产生一个空的GUID,而不是默认生成的一个作为与Guid.NewGuid()?

是否有一个空的Guid?

特殊用途
解决方案
  

为何无参数的Guid构造函数产生一个空的GUID,而不是默认生成的一个作为与Guid.NewGuid()?

简短的回答:因为语言/运行时没有让Guid类型的设计者定义默认的构造函数

这不仅是传统的价值是默认构造的结构是零,你根本无法定义默认构造函数的结构。当你说新的GUID()运行时给你,所有的字段都被初始化为它们的默认值的新对象:<一href="http://msdn.microsoft.com/en-us/library/ah19swz4%28VS.71%29.aspx">http://msdn.microsoft.com/en-us/library/ah19swz4%28VS.71%29.aspx

有些理由可以在这里找到:<一href="http://www.yoda.arachsys.com/csharp/faq/#struct.constructors">http://www.yoda.arachsys.com/csharp/faq/#struct.constructors

Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()?

Is there a particular use for an empty Guid?

解决方案

Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()?

Short answer: Because the language/runtime didn't let the designer of the Guid type define a default constructor.

It's not only conventional that the value of a "default-constructed" struct is zero, you simply cannot define a default constructor for a struct. When you say new Guid() the runtime gives you a new object where all the fields are initialized to their default values: http://msdn.microsoft.com/en-us/library/ah19swz4%28VS.71%29.aspx

Some rationale can be found here: http://www.yoda.arachsys.com/csharp/faq/#struct.constructors

这篇关于为什么无参数的Guid构造产生一个空的GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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