guid/uuid生成方法总结

部分资料摘抄自 微软官网 摘抄地址

Guid 结构

表示全局唯一标识符 (GUID)。

构造函数

名称说明
Guid(Byte[])使用指定的字节数组初始化 Guid 类的新实例。
Guid(String)使用指定字符串所表示的值初始化 Guid 类的新实例。
Guid(Int32, Int16, Int16, Byte[])使用指定的整数和字节数组初始化 Guid 类的新实例。
Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)使用指定的整数和字节初始化 Guid 类的新实例。
Guid(UInt32, UInt16, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)使用指定的无符号整数和字节初始化 Guid 类的新实例。

方法

名称说明
CompareTo(Guid)将此实例与指定 Guid 对象进行比较并返回对其相对值的指示。
CompareTo(Object)将此实例与指定对象进行比较并返回一个对二者的相对值的指示。
Equals(Guid)返回一个值,该值指示此实例和指定的 Guid 对象是否表示相同的值。
Equals(Object)返回一个值,该值指示此实例是否与指定的对象相等。 (重写 ValueType.Equals(Object)。)
Finalize允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode返回此实例的哈希代码。 (重写 ValueType.GetHashCode()。)
GetType获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone创建当前 Object 的浅表副本。 (继承自 Object。)
NewGuid初始化 Guid 类的新实例。
Parse将 GUID 的字符串表示形式转换为等效的 Guid 结构。
ParseExact将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。
ToByteArray返回包含此实例的值的 16 元素字节数组。
ToString()返回注册表格式的此实例值的字符串表示形式。 (重写 ValueType.ToString()。)
ToString(String)根据所提供的格式说明符,返回此 Guid 实例值的字符串表示形式。
ToString(String, IFormatProvider)根据所提供的格式说明符和区域性特定的格式信息,返回 Guid 类的此实例值的字符串表示形式。
TryParse将 GUID 的字符串表示形式转换为等效的 Guid 结构。
TryParseExact将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。

运算符

名称说明
Equality指示两个指定的 Guid 对象的值是否相等。
Inequality指示两个指定的 Guid 对象的值是否不相等。

字段

名称说明
EmptyGuid 类的只读实例,其值保证均为零。

示例demo

using System;
using System.Runtime.InteropServices;

// Guid for the interface IMyInterface.
[Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")]
interface IMyInterface
{
    void MyMethod();
}

// Guid for the coclass MyTestClass.
[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public class MyTestClass : IMyInterface
{
    // Run regasm on this assembly to create .reg and .tlb files.
    // Reg file can be used to register this coclass in the registry.
    // Tlb file will be used to do interop.

    public void MyMethod() {}

    public static void Main( string []args )
    {
        // Example addresses the following in System.Runtime.InterOpServices.GuidAttribute.
        // How to specify the attribute on interface/coclass.
        // Retrieve the GuidAttribute from an interface/coclass.
        // Value property on GuidAttribute class.

        // Example addresses the following in System.Guid.
        // Constructor Guid(string).
        // Constructor Guid(ByteArray).
        // Equals.
        // Operator ==.
        // CompareTo.

        Attribute IMyInterfaceAttribute = Attribute.GetCustomAttribute( typeof( IMyInterface ), typeof( GuidAttribute ) );

        // The Value property of GuidAttribute returns a string. 
        System.Console.WriteLine( "IMyInterface Attribute: " + ((GuidAttribute)IMyInterfaceAttribute).Value );    

        // Using the string to create a guid.
        Guid myGuid1 = new Guid( ((GuidAttribute)IMyInterfaceAttribute).Value );
        // Using a byte array to create a guid.
        Guid myGuid2 = new Guid ( myGuid1.ToByteArray() );

        // Equals is overridden and so value comparison is done though references are different.
        if ( myGuid1.Equals( myGuid2 ) )
            System.Console.WriteLine( "myGuid1 equals myGuid2" );
        else
            System.Console.WriteLine( "myGuid1 not equals myGuid2" );

        // Equality operator can also be used to determine if two guids have same value.
        if ( myGuid1 == myGuid2 )
            System.Console.WriteLine( "myGuid1 == myGuid2" );
        else
            System.Console.WriteLine( "myGuid1 != myGuid2" );

        // CompareTo returns 0 if the guids have same value.
        if ( myGuid1.CompareTo( myGuid2 ) == 0 )
            System.Console.WriteLine( "myGuid1 compares to myGuid2" );
        else
            System.Console.WriteLine( "myGuid1 does not compare to myGuid2" );

        System.Console.ReadLine();

        //Output.
        //IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
        //myGuid1 equals myGuid2
        //myGuid1 == myGuid2
        //myGuid1 compares to myGuid2
    }
}

常用方法

  • Guid.NewGuid创建一个新的 guid,也叫uuid

// This code example demonstrates the Guid.NewGuid() method.

using System;

class Sample 
{
    public static void Main() 
    {
    Guid g;
// Create and display the value of two GUIDs.
    g = Guid.NewGuid();
    Console.WriteLine(g);
    Console.WriteLine(Guid.NewGuid());
    }
}

/*
This code example produces the following results:

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7

*/

其他常见用法说明

string str = System.Guid.NewGuid().ToString("N") + "|"

+ System.Guid.NewGuid().ToString("D") + "|"

+ System.Guid.NewGuid().ToString("B") + "|"

+ System.Guid.NewGuid().ToString("P");
        Response.Write(str);


  • 返回的结果:

ece4f4a60b764339b94a07c84e338a27|
5bf99df1-dc49-4023-a34a-7bd80a42d6bb|

{2280f8d7-fd18-4c72-a9ab-405de3fcfbc9}|

(25e6e09f-fb66-4cab-b4cd-bfb429566549)

说明符返回值的格式示例格式
N32 位:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
D由连字符分隔的 32 位数字:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
B括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
P括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值