Guid.NewGuid()与新Guid()

本文翻译自:Guid.NewGuid() vs. new Guid()

What's the difference between Guid.NewGuid() and new Guid() ? Guid.NewGuid()new Guid()什么区别?

Which one is preferred? 哪一个更受欢迎?


#1楼

参考:https://stackoom.com/question/o5ep/Guid-NewGuid-与新Guid


#2楼

new Guid() makes an "empty" all-0 guid (00000000-0000-0000-0000-000000000000 is not very useful). new Guid()使得一个“空”全0-guid(00000000-0000-0000-0000-000000000000不是很有用)。

Guid.NewGuid() makes an actual guid with a unique value, what you probably want. Guid.NewGuid()制作一个具有独特价值的实际guid,你可能想要的。


#3楼

Guid.NewGuid() creates a new UUID using an algorithm that is designed to make collisions very, very unlikely. Guid.NewGuid()使用一种算法创建一个新的UUID,该算法旨在使冲突变得非常非常不可能。

new Guid() creates a UUID that is all-zeros. new Guid()创建一个全零的UUID。

Generally you would prefer the former, because that's the point of a UUID (unless you're receiving it from somewhere else of course). 一般来说,你更喜欢前者,因为这是UUID的重点(除非你当然是从其他地方接收它)。

There are cases where you do indeed want an all-zero UUID, but in this case Guid.Empty or default(Guid) is clearer about your intent, and there's less chance of someone reading it expecting a unique value had been created. 在某些情况下,您确实需要一个全零的UUID,但在这种情况下, Guid.Emptydefault(Guid)更清楚您的意图,并且有人在阅读它时期望创建一个唯一值的可能性更小。

In all, new Guid() isn't that useful due to this lack of clarity, but it's not possible to have a value-type that doesn't have a parameterless constructor that returns an all-zeros-and-nulls value. 总而言之,由于缺乏清晰度, new Guid()并没有那么有用,但是不可能有一个没有无参数构造函数的值类型返回一个全零和空值。

Edit: Actually, it is possible to have a parameterless constructor on a value type that doesn't set everything to zero and null, but you can't do it in C#, and the rules about when it will be called and when there will just be an all-zero struct created are confusing, so it's not a good idea anyway. 编辑:实际上,有可能在值类型上有一个无参数构造函数,它不会将所有内容设置为零和null,但是你不能在C#中执行它,以及关于何时调用它以及何时将调用它的规则只是创建一个全零结构是令人困惑的,所以无论如何它都不是一个好主意。


#4楼

[I understand this is an old thread, just adding some more detail] The two answers by Mark and Jon Hanna sum up the differences, albeit it may interest some that [我明白这是一个老线程,只是添加更多细节] Mark和Jon Hanna的两个答案总结了差异,尽管它可能会引起一些人的兴趣。

Guid.NewGuid()

Eventually calls CoCreateGuid (a COM call to Ole32) (reference here ) and the actual work is done by UuidCreate . 最后调用CoCreateGuid(对Ole32的COM调用)( 这里引用),实际工作由UuidCreate完成。

Guid.Empty is meant to be used to check if a Guid contains all zeroes. Guid.Empty用于检查Guid是否包含全零。 This could also be done via comparing the value of the Guid in question with new Guid() 这也可以通过将有问题的Guid的值与新的Guid()进行比较来完成

So, if you need a unique identifier , the answer is Guid.NewGuid() 因此,如果您需要唯一标识符 ,答案是Guid.NewGuid()


#5楼

Guid.NewGuid() , as it creates GUIDs as intended. Guid.NewGuid() ,因为它按预期创建GUID。

Guid.NewGuid() creates an empty Guid object, initializes it by calling CoCreateGuid and returns the object. Guid.NewGuid()创建一个空的Guid对象,通过调用CoCreateGuid对其进行初始化并返回该对象。

new Guid() merely creates an empty GUID (all zeros, I think). new Guid()只创建一个空的GUID(我认为全部为零)。

I guess they had to make the constructor public as Guid is a struct . 我猜他们必须使构造函数公开,因为Guid是一个struct

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值