NewGuid 与 System.Guid.NewGuid().ToString(“D”);

发布于 2024-12-06 07:51:29 字数 111 浏览 0 评论 0 原文

使用 NewGuid();System.Guid.NewGuid().ToString("D"); 生成 GUID 时是否有区别,或者它们是同一件事?

Is there a difference when you generate a GUID using NewGuid(); vs System.Guid.NewGuid().ToString("D"); or they are the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜无邪 2024-12-13 07:51:30

我意识到这个问题已经有了一个公认的答案,但我认为分享一些有关格式化指南的信息会很有用。

ToString()(无参数)方法使用此格式格式化 GUID :

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

ToString(string format) 方法 将 guid 格式化为以下几种方法之一:

"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)
"D" - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 digits separated by hyphens)
"B" - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (same as "D" with addition of braces)
"P" - (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) (same as "D" with addition of parentheses)
"X" - {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

调用 Guid.ToString("D") 与调用 Guid.ToString() 产生相同的结果。

正如其他答案中提到的,指南本身没有格式。它只是一个值。请注意,您可以使用 NewGuid 或使用 guid 的构造函数创建 guid。使用 NewGuid,您无法控制 guid 的值。使用 guid 的构造函数,您可以控制该值。如果您已经有了 guid 的字符串表示形式(也许您从数据库中读取它)或者您希望在开发过程中更轻松地解释 guid,那么使用构造函数会很有用。您还可以使用解析ParseExactTryParseTryParseExact 方法。

因此,您可以像这样创建指南:

Guid g1 = Guid.NewGuid(); //Get a Guid without any control over the contents
Guid g2 = new Guid(new string('A',32)); //Get a Guid where all digits == 'A'
Guid g3 = Guid.Parse(g1.ToString());
Guid g4 = Guid.ParseExact(g1.ToString("D"),"D");
Guid g5;
bool b1 = Guid.TryParse(g1.ToString(), out g5);
Guid g6;
bool b2 = Guid.TryParseExact(g1.ToString("D"),"D", out g6);

I realize that this question already has an accepted answer, but I thought it would be useful to share some information about formatting guids.

The ToString() (no parameters) method formats a guid using this format:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

The ToString(string format) method formats a guid in one of several ways:

"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits)
"D" - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 digits separated by hyphens)
"B" - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (same as "D" with addition of braces)
"P" - (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) (same as "D" with addition of parentheses)
"X" - {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Calling Guid.ToString("D") yields the same result as calling Guid.ToString().

As mentioned in the other answers, the guid itself has no format. It is just a value. Note, that you can create guids using NewGuid or using the guid's constructor. Using NewGuid, you have no control over the value of the guid. Using the guid's constructor, you can control the value. Using the constructor is useful if you already have a string representation of a guid (maybe you read it from a database) or if you want to make it easier to interpret a guid during development. You can also use the Parse, ParseExact, TryParse, and TryParseExact methods.

So, you can create guids like this:

Guid g1 = Guid.NewGuid(); //Get a Guid without any control over the contents
Guid g2 = new Guid(new string('A',32)); //Get a Guid where all digits == 'A'
Guid g3 = Guid.Parse(g1.ToString());
Guid g4 = Guid.ParseExact(g1.ToString("D"),"D");
Guid g5;
bool b1 = Guid.TryParse(g1.ToString(), out g5);
Guid g6;
bool b2 = Guid.TryParseExact(g1.ToString("D"),"D", out g6);
兔姬 2024-12-13 07:51:30

Guid.NewGuid().ToString() 是 GUID 的字符串表示形式,即返回 string 对象,而 Guid.NewGuid() 返回 Guid 数据类型。

Guid.NewGuid().ToString() is string representation of GUID, i.e. returns string object, while Guid.NewGuid() returns Guid datatype.

总以为 2024-12-13 07:51:30

使用 System.Guid.NewGuid() 您将获得 Guid 类型的对象

使用 System.Guid.NewGuid().ToString("D");< /code> 您将获得 Guid 对象的字符串表示形式

另外,据我所知, .ToString("D").ToString() 之间没有区别代码>

Using System.Guid.NewGuid() you will get a object of Guid type

Using System.Guid.NewGuid().ToString("D"); you will get the string representation of Guid object

Also as I know no difference between .ToString("D") and .ToString()

无名指的心愿 2024-12-13 07:51:30

两者的生成算法必须相同,因为 System.Guid.NewGuid().ToString("D") 正在调用 System.Guid.NewGuid(),然后对结果调用 ToString ,即,您的两个示例都调用相同的方法来生成 guid。至于比较“格式” - 这是没有意义的,因为 System.Guid.NewGuid() 没有与 System.Guid.NewGuid() 相同的“格式” .ToString("D") - 只有通过调用 ToString 方法,您才能为 guid 的内部表示形式提供外部字符串格式。字符串采用的格式取决于传递给字符串方法的参数。

The generation algorithm has to be the same for both, because System.Guid.NewGuid().ToString("D") is calling System.Guid.NewGuid(), and then calling ToString on the result, i.e., both of your examples are calling the same method to generate the guid. As to comparing the "format" - this does not make sense because System.Guid.NewGuid() does not have a "format" in the same way as System.Guid.NewGuid().ToString("D") - it is only by calling the ToString method that you give the internal representation of the guid an external, string format. The format the string takes will depend on the argument you pass to the string method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文