guid/uuid生成方法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ITzhongzi/article/details/80907898

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

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 对象的值是否不相等。

字段

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

示例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)

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

猜你喜欢

转载自blog.csdn.net/ITzhongzi/article/details/80907898
今日推荐