C#如何创建Guid值?
我们的结构体的一个字段是 Guid
类型。如何为其生成有效值?
One field of our struct is Guid
type. How to generate a valid value for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们的结构体的一个字段是 Guid
类型。如何为其生成有效值?
One field of our struct is Guid
type. How to generate a valid value for it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
有两种方法
或
同时使用 Guid 类,第一种创建 Guid 对象,第二种创建 Guid 字符串。
There are two ways
or
both use the Guid class, the first creates a Guid Object, the second a Guid string.
Guid.NewGuid()
创建一个新的随机 GUID。Guid.NewGuid()
creates a new random guid.Guid.NewGuid() 将创建一个
Guid.NewGuid() will create one
创建一个“空”全 0 guid,例如
00000000-0000-0000-0000-000000000000
。或
To 创建一个具有唯一值的实际指南,这可能是您想要的。
To makes an "empty" all-0 guid like
00000000-0000-0000-0000-000000000000
.or
To makes an actual guid with a unique value, what you probably want.
嘿,它是一个“有效”的Guid,尽管不是很有用。
(如果您不知道,则 guid 全部为零。有时,如果您不想使用可为 null 的 Guid,则需要指示没有 guid)
Hey, its a 'valid', although not very useful, Guid.
(the guid is all zeros, if you don't know. Sometimes this is needed to indicate no guid, in cases where you don't want to use a nullable Guid)
如果您想创建一个“所需的”Guid,您可以执行
类似1A3B944E-3632-467B-A53A-206305310BAE
的操作。If you want to create a "desired" Guid you can do
where
<guidValue>
would be something like1A3B944E-3632-467B-A53A-206305310BAE
.还有 ShortGuid - C# 中的较短且 url 友好的 GUID 类。它是
作为 Nuget 提供。更多信息 在这里。
用法:
这会生成一个新的 guid,使用该 guid 创建一个 ShortGuid,并在控制台中显示两个等效值。结果将类似于:
There's also ShortGuid - A shorter and url friendly GUID class in C#. It's
available as a Nuget. More information here.
Usage:
This produces a new guid, uses that guid to create a ShortGuid, and displays the two equivalent values in the console. Results would be something along the lines of:
使用 '
You need:
主要用于任何 C# 代码。
如果出现任何问题,请确保Guid未被覆盖。
Using '
You need:
Which mainly used in any C# code.
If any issue, make sure Guid is not overwritten.
如果您在 Reflection C# 中使用它,您可以从 property 属性中获取 guid,如下所示
If you are using this in the Reflection C#, you can get the guid from the property attribute as follows
这真的很容易。 .Net框架提供了一个内置函数来创建和解析GUIDS。这在 System 命名空间和静态 Guid 类中可用。
要创建 GUID,只需使用下面的代码:
要将 GUID 字符串解析为 GUID,请使用下面的代码:
如果您只想创建一个新指南并在应用程序中使用它,只需使用 在线 GUID 生成器 工具在线为您自己创建新的 GUID。
It's really easy. The .Net framework provides an in-built function to create and parse GUIDS. This is available in the System namespace and the static Guid class.
To create a GUID just use the code below:
To parse a GUID string as a GUID, use the code below:
If you just want to create a new guide and just use it in your application, just use one of the online GUID Generator tools online to create yourself a new guid.
如果您使用ABP框架,建议使用IGuidGenerator.Create()而不是Guid.NewGuid()。这是为了确保 GUID 是连续的,从而缩短数据库的读/写时间。
https://docs.abp.io/en/abp/latest/ Guid-Generation#iguidgenerator
If you are using the ABP framework, it is recommended to use IGuidGenerator.Create() instead of Guid.NewGuid(). This is to ensure GUIDs are sequential, improving read/write times on the database.
https://docs.abp.io/en/abp/latest/Guid-Generation#iguidgenerator