C#(019):C#中的Guid(C # (019): guid in C #)

原文:[Guid.ToString 方法 (System) | Microsoft Docs](https://docs.microsoft.com/zh-
cn/dotnet/api/system.guid.tostring?view=net-5.0)

一、全局唯一标识符(GUID,Globally Unique Identifier)

什么是GUID

也称作 UUID(Universally Unique IDentifier) 。

GUID是一种由算法生成的二进制长度为128位的数字标识符。GUID主要用于在拥有多个节点、多台计算机的网络或系统中。

为什么要使用GUID

在理想情况下,任何计算机和计算机集群都不会生成两个相同的GUID。GUID
的总数达到了2128(3.4×1038)个,所以随机生成两个相同GUID的可能性非常小,但并不为0。GUID一词有时也专指微软对UUID标准的实现。

(1). GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。

通常平台会提供生成GUID的API。生成算法很有意思,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。GUID的唯一缺陷在于生成的结果串会比较大。

(2). GUID永远是方便的

对于程序开发的各个方面,.NET Framework简化了建立和处理GUID数值的过程。在.NET程序需要的地方,这一功能很容易地生成唯一的数值。

二、Guid转换为字符串

    Guid u= Guid.NewGuid();
    var uuid = u.ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
    Console.WriteLine(uuid);
    
    var uuidD = u.ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
    Console.WriteLine(uuidD);
    
    var uuidN = u.ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09   
    Console.WriteLine(uuidN);
    
    var uuidB = u.ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}  
    Console.WriteLine(uuidB);
    
    var uuidP = u.ToString("P"); //  (ade24d16-db0f-40af-8794-1e08e2040df3)  
    Console.WriteLine(uuidP);
    
    
    var uuidX = u.ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}} 
    Console.WriteLine(uuidX);

返回:
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be80954a3d81af6e8f1fe25726
{c6bb19be-8095-4a3d-81af-6e8f1fe25726}
(c6bb19be-8095-4a3d-81af-6e8f1fe25726)
{0xc6bb19be,0x8095,0x4a3d,{0x81,0xaf,0x6e,0x8f,0x1f,0xe2,0x57,0x26}}

返回:

c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be80954a3d81af6e8f1fe25726
{c6bb19be-8095-4a3d-81af-6e8f1fe25726}
(c6bb19be-8095-4a3d-81af-6e8f1fe25726)
{0xc6bb19be,0x8095,0x4a3d,{0x81,0xaf,0x6e,0x8f,0x1f,0xe2,0x57,0x26}}

三、字符串转换为Guid

字符串转换为Guid的两种常用方式:以下几种字符串都行。

    //string str = "21140D4D-9CB3-41C1-99DC-30B4BCBE1989";
    //string str = "{21140D4D-9CB3-41C1-99DC-30B4BCBE1989}";
    //string str = "21140D4D9CB341C199DC30B4BCBE1989";
    string str = "21140d4d9cb341c199dc30b4bcbe1989";
    // 方式1
    Guid g1 = new Guid(str);
    
    // 方式2, .net framework 4.0 以上
    Guid g2 = Guid.Parse(str);
    
    Console.WriteLine(g1.ToString());
    Console.WriteLine(g2.ToString());

返回:
21140d4d-9cb3-41c1-99dc-30b4bcbe1989
21140d4d-9cb3-41c1-99dc-30b4bcbe1989

返回:

21140d4d-9cb3-41c1-99dc-30b4bcbe1989
21140d4d-9cb3-41c1-99dc-30b4bcbe1989

四、与SQL中的uniqueIdentifier区别

C#中的System.Guid.NewGuid()返回的格式是小写形式, SQL Server中的NEWID()返回的格式是大写形式.

    select newid() ;--返回: DECC53C8-62CF-45E4-A2E2-138BDAE0AE5E
————————

原文:[Guid.ToString 方法 (System) | Microsoft Docs](https://docs.microsoft.com/zh-
cn/dotnet/api/system.guid.tostring?view=net-5.0)

一、全局唯一标识符(GUID,Globally Unique Identifier)

什么是GUID

也称作 UUID(Universally Unique IDentifier) 。

GUID is a digital identifier with a binary length of 128 bits generated by the algorithm. GUID is mainly used in networks or systems with multiple nodes and computers.

Why use a guid

Ideally, no computer or cluster of computers would generate two identical guids. GUID
The total reached 2128 (3.4 × 1038), so the possibility of randomly generating two identical guids is very small, but it is not 0. The term guid sometimes refers specifically to Microsoft’s implementation of the UUID standard.

(1). Guid (global uniform identifier) refers to the number generated on one machine. It ensures that it is unique to all machines in the same time and space.

Usually, the platform provides an API for generating guids. The generation algorithm is very interesting. It uses Ethernet card address, nanosecond time, chip ID code and many possible numbers. The only drawback with guids is that the resulting string will be large.

(2). GUID is always convenient

For all aspects of program development Net framework simplifies the process of establishing and processing guid values. Yes Net program needs, this function can easily generate unique values.

2、 Guid to string

    Guid u= Guid.NewGuid();
    var uuid = u.ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
    Console.WriteLine(uuid);
    
    var uuidD = u.ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
    Console.WriteLine(uuidD);
    
    var uuidN = u.ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09   
    Console.WriteLine(uuidN);
    
    var uuidB = u.ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}  
    Console.WriteLine(uuidB);
    
    var uuidP = u.ToString("P"); //  (ade24d16-db0f-40af-8794-1e08e2040df3)  
    Console.WriteLine(uuidP);
    
    
    var uuidX = u.ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}} 
    Console.WriteLine(uuidX);

返回:
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be80954a3d81af6e8f1fe25726
{c6bb19be-8095-4a3d-81af-6e8f1fe25726}
(c6bb19be-8095-4a3d-81af-6e8f1fe25726)
{0xc6bb19be,0x8095,0x4a3d,{0x81,0xaf,0x6e,0x8f,0x1f,0xe2,0x57,0x26}}

return:

c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be-8095-4a3d-81af-6e8f1fe25726
c6bb19be80954a3d81af6e8f1fe25726
{c6bb19be-8095-4a3d-81af-6e8f1fe25726}
(c6bb19be-8095-4a3d-81af-6e8f1fe25726)
{0xc6bb19be,0x8095,0x4a3d,{0x81,0xaf,0x6e,0x8f,0x1f,0xe2,0x57,0x26}}

3、 Convert string to guid

There are two common ways to convert strings to guids: the following strings are OK.

    //string str = "21140D4D-9CB3-41C1-99DC-30B4BCBE1989";
    //string str = "{21140D4D-9CB3-41C1-99DC-30B4BCBE1989}";
    //string str = "21140D4D9CB341C199DC30B4BCBE1989";
    string str = "21140d4d9cb341c199dc30b4bcbe1989";
    // 方式1
    Guid g1 = new Guid(str);
    
    // 方式2, .net framework 4.0 以上
    Guid g2 = Guid.Parse(str);
    
    Console.WriteLine(g1.ToString());
    Console.WriteLine(g2.ToString());

返回:
21140d4d-9cb3-41c1-99dc-30b4bcbe1989
21140d4d-9cb3-41c1-99dc-30b4bcbe1989

return:

21140d4d-9cb3-41c1-99dc-30b4bcbe1989
21140d4d-9cb3-41c1-99dc-30b4bcbe1989

四、与SQL中的uniqueIdentifier区别

System in C # Guid. The format returned by newguid() is lowercase, and the format returned by newid() in SQL server is uppercase

    select newid() ;--返回: DECC53C8-62CF-45E4-A2E2-138BDAE0AE5E