本文整理匯總了C#中System.Guid結構體的典型用法代碼示例。如果您正苦於以下問題:C# Guid結構體的具體用法?C# Guid怎麽用?C# Guid使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
Guid結構體屬於System命名空間,在下文中一共展示了Guid結構體的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MyMethod
//引入命名空間
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
{
public void MyMethod() {}
public static void Main( string []args )
{
GuidAttribute IMyInterfaceAttribute = (GuidAttribute) Attribute.GetCustomAttribute(typeof(IMyInterface), typeof(GuidAttribute));
System.Console.WriteLine("IMyInterface Attribute: " + IMyInterfaceAttribute.Value );
// Use the string to create a guid.
Guid myGuid1 = new Guid(IMyInterfaceAttribute.Value );
// Use a byte array to create a guid.
Guid myGuid2 = new Guid(myGuid1.ToByteArray());
if (myGuid1.Equals(myGuid2))
System.Console.WriteLine("myGuid1 equals myGuid2");
else
System.Console.WriteLine("myGuid1 does not equal 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" );
}
}
輸出:
IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4 myGuid1 equals myGuid2 myGuid1 == myGuid2
示例2: new Guid
//引入命名空間
using System;
public class MainClass {
public static void Main() {
Guid g1 = new Guid("{0d57629c-7d6e-4847-97cb-9e2fc25083fe}");
Guid g2 = new Guid("0d57629c7d6e484797cb9e2fc25083fe");
Console.WriteLine(g1 == g2); // True
}
}