C# Guid.NewGuid() 与 new Guid()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11938151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Guid.NewGuid() vs. new Guid()
提问by OscarRyz
What's the difference between Guid.NewGuid()and new Guid()?
Guid.NewGuid()和 和有new Guid()什么区别?
Which one is preferred?
哪一个是首选?
采纳答案by MarkPflug
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,你可能想要什么。
回答by Jon Hanna
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.Emptyor 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.Empty或者default(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# 中执行此操作,以及有关何时调用它以及何时调用它的规则只是创建一个全零结构会令人困惑,所以无论如何这都不是一个好主意。
回答by Sudhanshu Mishra
[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. This could also be done via comparing the value of the Guid in question with new Guid()
Guid.Empty 用于检查 Guid 是否包含全零。这也可以通过将有问题的 Guid 的值与new Guid()进行比较来完成
So, if you need a unique identifier, the answer is Guid.NewGuid()
所以,如果你需要一个唯一标识符,答案是Guid.NewGuid()
回答by Sharath
Guid.NewGuid(), as it creates GUIDs as intended.
Guid.NewGuid(),因为它会按预期创建 GUID。
Guid.NewGuid()creates an empty Guidobject, initializes it by calling CoCreateGuidand 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 Guidis a struct.
我猜他们不得不做出的构造公众Guid是一个struct。

