GUID概念



GUID概念

 

 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) 。 GUID是一个通过特定算法产生的二进制长度为128位的数字标识符,用于指示产品的唯一性。GUID 主要用于在拥有多个节点、多台计算机的网络或系统中,分配必须具有唯一性的标识符。

在 Windows 平台上,GUID 广泛应用于微软的产品中,用于标识如如注册表项、类及接口标识、数据库、系统目录等对象

 

GUID格式

 GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。

 

 

GUID特点

★GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。 ★世界上的任何两台计算机都不会生成重复的 GUID 值。★需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。 ★GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。

 

 

GUID生成算法

网上流传,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。有待验证。

 

 

GUID例子

1.注册表,GUIDs被用于整个Windows环境。当你在一个Windows系统中仔细阅读注册表时,你可以看到GUIDs被广泛用于唯一识别程序。特别地,它们作为程序的Ids集中在HKEY_CLASSES_ROOT部分(AppID键)。

 

2.C#,Sqlserver的唯一表示,当基于web的应用程序,并发量很大的情况下,并发访问上千以上时,在数据中,使用 int id identiy(1,1),出现相同的概率会大,GUID,就有所改善。

class Program
    {
        // 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

                //GENERATE GUIDS
                //for (int i = 0; i <100; i++)
                //{
                //    Console.WriteLine(System.Guid.NewGuid().ToString());
                //}
              
                //Console.ReadLine();
               


            }


        }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值