Guid Guid Guid Guid Struct

定義

表示全域唯一識別項 (GUID)。Represents a globally unique identifier (GUID).

public value class Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Guid : IComparable, IComparable<Guid>, IEquatable<Guid>, IFormattable
type Guid = struct
    interface IFormattable
Public Structure Guid
Implements IComparable, IComparable(Of Guid), IEquatable(Of Guid), IFormattable
繼承
屬性
實作

範例

下列範例會使用 System.Runtime.InteropServices.GuidAttribute 類別,將 GUID 指派給介面和使用者定義的類別。The following example uses the System.Runtime.InteropServices.GuidAttribute class to assign a GUID to an interface and to a user-defined class. 它會藉由呼叫 GetCustomAttribute 方法來抓取 GUID 的值,並將它與其他兩個 Guid 進行比較,以判斷它們是否相等。It retrieves the value of the GUID by calling the GetCustomAttribute method, and compares it with two other GUIDs to determine whether they are equal.

using namespace System;
using namespace System::Runtime::InteropServices;

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


// Guid for the coclass MyTestClass.
[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public ref class MyTestClass: public IMyInterface
{
public:
   virtual void MyMethod(){}
};

int main()
{
   Attribute^ IMyInterfaceAttribute = Attribute::GetCustomAttribute( IMyInterface::typeid, GuidAttribute::typeid );

   // The Value property of GuidAttribute returns a string. 
   System::Console::WriteLine( String::Concat(  "IMyInterface Attribute: ", (dynamic_cast<GuidAttribute^>(IMyInterfaceAttribute))->Value ) );

   // Using the string to create a guid.
   Guid myGuid1 = Guid(dynamic_cast<GuidAttribute^>(IMyInterfaceAttribute)->Value);

   // Using a byte array to create a guid.
   Guid myGuid2 = Guid(myGuid1.ToByteArray());

   // Equals is overridden to perform a value comparison.
   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" );
}
// The example displays the following output:
//       IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
//       myGuid1 equals myGuid2
//       myGuid1 == myGuid2
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" );
    }
}
// The example displays the following output:
//       IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
//       myGuid1 equals myGuid2
//       myGuid1 == myGuid2
Imports System.Runtime.InteropServices

' Guid for the interface IMyInterface.
<Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")> _
Interface IMyInterface
    Sub MyMethod()
End Interface

' Guid for the coclass MyTestClass.
<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")> _
Public Class MyTestClass
    Implements IMyInterface

    Public Sub MyMethod() Implements IMyInterface.MyMethod
    End Sub

    Public Shared Sub Main()
        Dim IMyInterfaceAttribute As GuidAttribute = CType(Attribute.GetCustomAttribute(GetType(IMyInterface), GetType(GuidAttribute)),
                                                           GuidAttribute)

        Console.WriteLine("IMyInterface Attribute: " + IMyInterfaceAttribute.Value)

        ' Use the string to create a guid.
        Dim myGuid1 As New Guid(IMyInterfaceAttribute.Value)
        ' Use a byte array to create a guid.
        Dim myGuid2 As New Guid(myGuid1.ToByteArray())

        If myGuid1.Equals(myGuid2) Then
            Console.WriteLine("myGuid1 equals myGuid2")
        Else
            Console.WriteLine("myGuid1 does not equal myGuid2")
        End If 

        ' The equality operator can also be used to determine if two guids have same value.
        If myGuid1.ToString() = myGuid2.ToString() Then
            Console.WriteLine("myGuid1 == myGuid2")
        Else
            Console.WriteLine("myGuid1 != myGuid2")
        End If
    End Sub
End Class
' The example displays the following output:
'       IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
'       myGuid1 equals myGuid2
'       myGuid1 == myGuid2

請注意,GuidAttribute 屬性通常用於應用程式中,以向 COM 公開類型。Note that the GuidAttribute attribute is typically used in an application to expose a type to COM. 如果您編譯此範例,您可以在產生的元件上執行元件註冊工具(Regasm) ,以建立登錄(.reg)和類型程式庫(.tlb)檔案。If you compile this example, you can run the Assembly Registration tool (Regasm.exe) on the generated assembly to create registry (.reg) and type library (.tlb) files. .Reg 檔案可以用來在登錄中註冊 coclass,而且 .tlb 檔案可以提供 COM Interop 的中繼資料。The .reg file can be used to register the coclass in the registry, and the .tlb file can provide metadata for COM interop.

備註

GUID 是128位整數(16個位元組),不論需要唯一識別碼,都可以在所有電腦和網路上使用。A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. 這種識別碼的可能性非常低。Such an identifier has a very low probability of being duplicated.

建構函式

Guid(Byte[]) Guid(Byte[]) Guid(Byte[]) Guid(Byte[])

使用指定的位元組陣列,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the specified array of bytes.

Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>) Guid(ReadOnlySpan<Byte>)

使用指定唯讀位元組範圍代表的值,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the value represented by the specified read-only span of bytes.

Guid(String) Guid(String) Guid(String) Guid(String)

使用指定字串表示的值,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the value represented by the specified string.

Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[]) Guid(Int32, Int16, Int16, Byte[])

使用指定的整數和位元組陣列,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the specified integers and byte array.

Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(Int32, Int16, Int16, Byte, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

使用指定的整數和位元組,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the specified integers and bytes.

Guid(UInt32, UInt16, Byte, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, Byte, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, Byte, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte) Guid(UInt32, UInt16, Byte, UInt16, Byte, Byte, Byte, Byte, Byte, Byte, Byte)

使用指定不帶正負號的整數 (Unsigned Integer) 和位元組,初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure by using the specified unsigned integers and bytes.

欄位

Empty Empty Empty Empty

Guid 結構的唯讀執行個體,其值全為零。A read-only instance of the Guid structure whose value is all zeros.

方法

CompareTo(Guid) CompareTo(Guid) CompareTo(Guid) CompareTo(Guid)

將這個執行個體與指定的 Guid 物件相比較,並傳回它們的相對值指示。Compares this instance to a specified Guid object and returns an indication of their relative values.

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。Compares this instance to a specified object and returns an indication of their relative values.

Equals(Guid) Equals(Guid) Equals(Guid) Equals(Guid)

傳回數值,指示這個執行個體和指定的 Guid 物件是否表示相同的值。Returns a value indicating whether this instance and a specified Guid object represent the same value.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

NewGuid() NewGuid() NewGuid() NewGuid()

初始化 Guid 結構的新執行個體。Initializes a new instance of the Guid structure.

Parse(String) Parse(String) Parse(String) Parse(String)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

將代表 GUID 的唯讀字元範圍轉換為對等的 Guid 結構。Converts a read-only character span that represents a GUID to the equivalent Guid structure.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

將 GUID 的字元範圍表示轉換為對等的 Guid 結構,但字串必須是指定的格式。Converts the character span representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

ParseExact(String, String) ParseExact(String, String) ParseExact(String, String) ParseExact(String, String)

將 GUID 的字串表示轉換為對等的 Guid 結構,但字串必須是指定的格式。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

ToByteArray() ToByteArray() ToByteArray() ToByteArray()

傳回 16 個元素的位元組陣列,位元組陣列會包含這個執行個體的值。Returns a 16-element byte array that contains the value of this instance.

ToString() ToString() ToString() ToString()

以登錄格式傳回這個執行個體的值的字串表示。Returns a string representation of the value of this instance in registry format.

ToString(String) ToString(String) ToString(String) ToString(String)

根據提供的格式規範,傳回這個 Guid 執行個體的值的字串表示。Returns a string representation of the value of this Guid instance, according to the provided format specifier.

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

根據提供的格式規範和特定文化特性格式資訊,傳回這個 Guid 類別執行個體值的字串表示。Returns a string representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>)

嘗試將目前 HUID 執行個體的格式設定為提供的字元範圍。Tries to format the current GUID instance into the provided character span.

TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid)

將包含 GUID 表示法的指定唯讀字元範圍轉換為對等的 Guid 結構。Converts the specified read-only span of characters containing the representation of a GUID to the equivalent Guid structure.

TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

將代表 GUID 的字元範圍轉換為對等的 Guid 結構,但字串必須是指定的格式。Converts span of characters representing the GUID to the equivalent Guid structure, provided that the string is in the specified format.

TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid)

將 GUID 的字串表示轉換為對等的 Guid 結構,但字串必須是指定的格式。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>) TryWriteBytes(Span<Byte>)

嘗試將目前的 GUID 執行個體寫入到位元組範圍。Tries to write the current GUID instance into a span of bytes.

運算子

Equality(Guid, Guid) Equality(Guid, Guid) Equality(Guid, Guid) Equality(Guid, Guid)

表示兩個指定之 Guid 物件的值是否相等。Indicates whether the values of two specified Guid objects are equal.

Inequality(Guid, Guid) Inequality(Guid, Guid) Inequality(Guid, Guid) Inequality(Guid, Guid)

表示兩個指定之 Guid 物件的值是否不相等。Indicates whether the values of two specified Guid objects are not equal.

明確介面實作

IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object)

將這個執行個體與指定的 Guid 物件相比較,並傳回它們的相對值指示。Compares this instance to a specified Guid object and returns an indication of their relative values.

IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider) IFormattable.ToString(String, IFormatProvider)

依據提供的格式指定名稱及文化特性 (Culture) 專用格式資訊,傳回此執行個體之值的字串表示。Returns a string representation of the value of this instance, according to the provided format specifier and culture-specific format information.

適用於