C#如何创建Guid值?

发布于 2024-08-23 20:38:33 字数 51 浏览 2 评论 0原文

我们的结构体的一个字段是 Guid 类型。如何为其生成有效值?

One field of our struct is Guid type. How to generate a valid value for it?

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

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

发布评论

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

评论(13

壹場煙雨 2024-08-30 20:38:33
Guid id = Guid.NewGuid();
Guid id = Guid.NewGuid();
神仙妹妹 2024-08-30 20:38:33

有两种方法

var guid = Guid.NewGuid();

var guid = Guid.NewGuid().ToString();

同时使用 Guid 类,第一种创建 Guid 对象,第二种创建 Guid 字符串。

There are two ways

var guid = Guid.NewGuid();

or

var guid = Guid.NewGuid().ToString();

both use the Guid class, the first creates a Guid Object, the second a Guid string.

与君绝 2024-08-30 20:38:33

Guid.NewGuid() 创建一个新的随机 GUID。

Guid.NewGuid() creates a new random guid.

猥琐帝 2024-08-30 20:38:33

Guid.NewGuid() 将创建一个

Guid.NewGuid() will create one

久夏青 2024-08-30 20:38:33

创建一个“空”全 0 guid,例如 00000000-0000-0000-0000-000000000000

var makeAllZeroGuID = new System.Guid();

var makeAllZeroGuID = System.Guid.Empty;

To 创建一个具有唯一值的实际指南,这可能是您想要的。

var uniqueGuID = System.Guid.NewGuid(); 

To makes an "empty" all-0 guid like 00000000-0000-0000-0000-000000000000.

var makeAllZeroGuID = new System.Guid();

or

var makeAllZeroGuID = System.Guid.Empty;

To makes an actual guid with a unique value, what you probably want.

var uniqueGuID = System.Guid.NewGuid(); 
一桥轻雨一伞开 2024-08-30 20:38:33
var guid = new Guid();

嘿,它是一个“有效”的Guid,尽管不是很有用。

(如果您不知道,则 guid 全部为零。有时,如果您不想使用可为 null 的 Guid,则需要指示没有 guid)

var guid = new 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)

孤云独去闲 2024-08-30 20:38:33

如果您想创建一个“所需的”Guid,您可以执行

var tempGuid = Guid.Parse("<guidValue>");

类似 1A3B944E-3632-467B-A53A-206305310BAE 的操作。

If you want to create a "desired" Guid you can do

var tempGuid = Guid.Parse("<guidValue>");

where <guidValue> would be something like 1A3B944E-3632-467B-A53A-206305310BAE.

孤城病女 2024-08-30 20:38:33
System.Guid desiredGuid = System.Guid.NewGuid();
System.Guid desiredGuid = System.Guid.NewGuid();
故事未完 2024-08-30 20:38:33

还有 ShortGuid - C# 中的较短且 url 友好的 GUID 类。它是
作为 Nuget 提供。更多信息 在这里

PM> Install-Package CSharpVitamins.ShortGuid

用法:

Guid guid = Guid.NewGuid();
ShortGuid sguid1 = guid; // implicitly cast the guid as a shortguid
Console.WriteLine(sguid1);
Console.WriteLine(sguid1.Guid);

这会生成一个新的 guid,使用该 guid 创建一个 ShortGuid,并在控制台中显示两个等效值。结果将类似于:

ShortGuid: FEx1sZbSD0ugmgMAF_RGHw 
Guid:      b1754c14-d296-4b0f-a09a-030017f4461f

There's also ShortGuid - A shorter and url friendly GUID class in C#. It's
available as a Nuget. More information here.

PM> Install-Package CSharpVitamins.ShortGuid

Usage:

Guid guid = Guid.NewGuid();
ShortGuid sguid1 = guid; // implicitly cast the guid as a shortguid
Console.WriteLine(sguid1);
Console.WriteLine(sguid1.Guid);

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:

ShortGuid: FEx1sZbSD0ugmgMAF_RGHw 
Guid:      b1754c14-d296-4b0f-a09a-030017f4461f
め可乐爱微笑 2024-08-30 20:38:33

使用 '

Guid guid = Guid.NewGuid();

You need:

using System;

主要用于任何 C# 代码。

如果出现任何问题,请确保Guid未被覆盖

Using '

Guid guid = Guid.NewGuid();

You need:

using System;

Which mainly used in any C# code.

If any issue, make sure Guid is not overwritten.

多彩岁月 2024-08-30 20:38:33

如果您在 Reflection C# 中使用它,您可以从 property 属性中获取 guid,如下所示

var propertyAttributes= property.GetCustomAttributes();
foreach(var attribute in propertyAttributes)
{
  var myguid= Guid.Parse(attribute.Id.ToString());
}


If you are using this in the Reflection C#, you can get the guid from the property attribute as follows

var propertyAttributes= property.GetCustomAttributes();
foreach(var attribute in propertyAttributes)
{
  var myguid= Guid.Parse(attribute.Id.ToString());
}


傾城如夢未必闌珊 2024-08-30 20:38:33

这真的很容易。 .Net框架提供了一个内置函数来创建和解析GUIDS。这在 System 命名空间和静态 Guid 类中可用。

要创建 GUID,只需使用下面的代码:

var newGuid = System.Guid.NewGuid(); 

要将 GUID 字符串解析为 GUID,请使用下面的代码:

var parsedGuid = System.Guid.Parse(guidString);

如果您只想创建一个新指南并在应用程序中使用它,只需使用 在线 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:

var newGuid = System.Guid.NewGuid(); 

To parse a GUID string as a GUID, use the code below:

var parsedGuid = System.Guid.Parse(guidString);

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.

从此见与不见 2024-08-30 20:38:33

如果您使用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

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