Guid.CompareTo 方法

定義

將這個執行個體與指定的物件或 Guid 相比較,並傳回它們的相對值指示。

多載

CompareTo(Guid)

將這個執行個體與指定的 Guid 物件相比較,並傳回它們的相對值指示。

CompareTo(Object)

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。

CompareTo(Guid)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將這個執行個體與指定的 Guid 物件相比較,並傳回它們的相對值指示。

public:
 virtual int CompareTo(Guid value);
public int CompareTo (Guid value);
abstract member CompareTo : Guid -> int
override this.CompareTo : Guid -> int
Public Function CompareTo (value As Guid) As Integer

參數

value
Guid

與這個執行個體相比較的物件。

傳回

帶正負號的數字,其指出這個執行個體與 value 的相對值。

傳回值 描述
負整數 這個執行個體小於 value
這個執行個體等於 value
正整數 這個執行個體大於 value

實作

範例

下列範例會呼叫 方法, CompareTo(Guid) 以比較 GUID 值與兩個類似的 GUID 值。

using System;

public class Example
{
   public static void Main()
   {
      Guid mainGuid = Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d");
      unchecked {
         Guid guid2 = new Guid(0x01e75c83, (short) 0xc6f5,
                               0x4192,
                               new Byte[] { 0xb5, 0x7e, 0x74, 0x27, 0xce, 0xc5, 0x56, 0x0c} );
         Guid guid3 = Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d");

         Console.WriteLine("{0} {1:F} {2}", mainGuid,
                           (Comparison) mainGuid.CompareTo(guid2), guid2);
         Console.WriteLine("{0} {1:F} {2}", mainGuid,
                           (Comparison) mainGuid.CompareTo(guid3), guid3);
      }
   }

   private enum Comparison
   { LessThan = -1, Equals = 0, GreaterThan = 1 }
}
// The example displays the following output:
//    01e75c83-c6f5-4192-b57e-7427cec5560d GreaterThan 01e75c83-c6f5-4192-b57e-7427cec5560c
//    01e75c83-c6f5-4192-b57e-7427cec5560d LessThan 01e75c84-c6f5-4192-b57e-7427cec5560d
open System

type Comparison =
    | ``Less Than`` = -1
    | Equals = 0
    | ``Greater Than`` = 1

let mainGuid = 
    Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"

let guid2 = Guid(0x01e75c83, 0xc6f5s, 0x4192s, [| 0xb5uy; 0x7euy; 0x74uy; 0x27uy; 0xceuy; 0xc5uy; 0x56uy; 0x0cuy |])
let guid3 = 
    Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d")

printfn $"{mainGuid} {mainGuid.CompareTo guid2 |> enum<Comparison> :F} {guid2}"
printfn $"{mainGuid} {mainGuid.CompareTo guid3 |> enum<Comparison> :F} {guid3}"

// The example displays the following output:
//    01e75c83-c6f5-4192-b57e-7427cec5560d Greater Than 01e75c83-c6f5-4192-b57e-7427cec5560c
//    01e75c83-c6f5-4192-b57e-7427cec5560d Less Than 01e75c84-c6f5-4192-b57e-7427cec5560d
Module Example
   Public Sub Main()
      Dim mainGuid As Guid = Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d")
      Dim guid2 As New Guid(&h01e75c83, 
                            BitConverter.ToInt16(new Byte() { &hf5, &hc6 }, 0), 
                            &h4192, 
                            new Byte() { &hb5, &h7e, &h74, &h27, &hce, &hc5, &h56, &h0c} )
      Dim guid3 As Guid = Guid.Parse("01e75c84-c6f5-4192-b57e-7427cec5560d")
      
      Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                        CType(mainGuid.CompareTo(guid2), Comparison), guid2)
      Console.WriteLine("{0} {1:F} {2}", mainGuid, 
                        CType(mainGuid.CompareTo(guid3), Comparison), guid3)
   End Sub
   
   Private Enum Comparison As Integer
      LessThan = -1
      Equals = 0
      GreaterThan = 1
   End Enum
End Module
' The example displays the following output:
'    01e75c83-c6f5-4192-b57e-7427cec5560d GreaterThan 01e75c83-c6f5-4192-b57e-7427cec5560c
'    01e75c83-c6f5-4192-b57e-7427cec5560d LessThan 01e75c84-c6f5-4192-b57e-7427cec5560d

備註

方法 CompareTo 會比較 GUID,就像是提供給 Guid(Int32, Int16, Int16, Byte[]) 建構函式的值一樣,如下所示:

  • 它會比較 UInt32 值,並在其不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 它會比較第一個 UInt16 值,並在結果不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 它會比較第二 UInt16 個值,並在結果不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 如果 執行下一個八 Byte 個值的位元組位元組比較。 當遇到第一個不相等配對時,它會傳回結果。 否則,它會傳回 0,表示兩 Guid 個值相等。

請注意,最後八個位元組會以反向順序出現在 的 Guid 字串表示中,從低位元組到高位元組。 例如,在值 「01e75c83-c6f5-4192-b57e-7427cec5560d」 的字串表示 Guid 中,最後八個位元組是 「b57e-7427cec5560d」。換句話說,最後八個位元組會以位元組位元組為基礎從左至右進行比較,從0xb5開始。

如果兩個 GUID 的元件值相等,則方法會比較下一個元件。 當它找到值不相等的元件時,它會傳回結果。

這個方法會實作 System.IComparable<T> 介面,並執行比 方法稍微好 Guid.CompareTo 一點,因為它不需要將 value 參數 Guid 轉換成值。

適用於

CompareTo(Object)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將這個執行個體與指定的物件相比較,並傳回它們的相對值指示。

public:
 virtual int CompareTo(System::Object ^ value);
public int CompareTo (object? value);
public int CompareTo (object value);
abstract member CompareTo : obj -> int
override this.CompareTo : obj -> int
Public Function CompareTo (value As Object) As Integer

參數

value
Object

要比較的物件或 null

傳回

帶正負號的數字,其指出這個執行個體與 value 的相對值。

傳回值 描述
負整數 這個執行個體小於 value
這個執行個體等於 value
正整數 這個執行個體大於 value,或者 valuenull

實作

例外狀況

value 不是 Guid

範例

下列範例會使用 屬性將 GuidAttribute GUID 指派給類別。 它會藉由呼叫 Attribute.GetCustomAttribute 方法,並將傳 GuidAttribute 回物件的 屬性傳遞 ValueParse 方法,以擷取這個 GUID 的值。 然後它會比較該 GUID 與值的陣列。

using System;
using System.Runtime.InteropServices;

[Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")]
public class Example
{
   public static void Main()
   {
      GuidAttribute guidAttr = (GuidAttribute) Attribute.GetCustomAttribute(typeof(Example),
                                                      typeof(GuidAttribute));
      Guid guidValue = Guid.Parse(guidAttr.Value);
      Object[] values = { null , 16,
                          Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d"),
                          guidValue };
      foreach (var value in values) {
         try {
            Console.WriteLine("{0} and {1}: {2}", guidValue,
                              value == null ? "null" : value,
                              guidValue.CompareTo(value));
         }
         catch (ArgumentException) {
            Console.WriteLine("Cannot compare {0} and {1}", guidValue,
                              value == null ? "null" : value);
         }
      }
   }
}
// The example displays the following output:
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and null: 1
//    Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0
open System
open System.Runtime.InteropServices

[<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")>]
type Example = class end

let guidAttr = 
    Attribute.GetCustomAttribute(typeof<Example>, typeof<GuidAttribute>) :?> GuidAttribute
    
let guidValue = 
    Guid.Parse guidAttr.Value

let values: obj[] =
    [| null; 16 
       Guid.Parse "01e75c83-c6f5-4192-b57e-7427cec5560d"
       guidValue |]

for value in values do
    try
        printfn $"{guidValue} and %A{value}: {guidValue.CompareTo value}"
    with :? ArgumentException ->
        printfn $"Cannot compare {guidValue} and %A{value}"

// The example displays the following output:
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and <null>: 1
//    Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
//    936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0
Imports System.Runtime.InteropServices

<Guid("936DA01F-9ABD-4d9d-80C7-02AF85C822A8")>
Module Example
   Public Sub Main()
      Dim guidAttr As GuidAttribute = CType(Attribute.GetCustomAttribute(GetType(Example), 
                                                      GetType(GuidAttribute)), GuidAttribute)
      Dim guidValue As Guid = Guid.Parse(guidAttr.Value)
      Dim values() As Object = { Nothing, 16, 
                               Guid.Parse("01e75c83-c6f5-4192-b57e-7427cec5560d"),
                               guidValue }
      For Each value In values
         Try
            Console.WriteLine("{0} and {1}: {2}", guidValue, 
                              If(value Is Nothing, "null", value),
                              guidValue.CompareTo(value))
         Catch e As ArgumentException
            Console.WriteLine("Cannot compare {0} and {1}", guidValue,
                              If(value Is Nothing, "null", value))
         End Try                     
      Next                         
   End Sub
End Module
' The example displays the following output:
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and null: 1
'    Cannot compare 936da01f-9abd-4d9d-80c7-02af85c822a8 and 16
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and 01e75c83-c6f5-4192-b57e-7427cec5560d: 1
'    936da01f-9abd-4d9d-80c7-02af85c822a8 and 936da01f-9abd-4d9d-80c7-02af85c822a8: 0

備註

參數 value 必須是 null 或 的 Guid 實例,否則會擲回例外狀況。 不論其值為何,任何 實例 Guid 都會被視為大於 null

方法 CompareTo 會比較 GUID,就像是提供給 Guid 建構函式的值一樣,如下所示:

  • 它會比較 Int32 值,並在其不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 它會比較第一個 Int16 值,並在結果不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 它會比較第二 Int16 個值,並在結果不相等時傳回結果。 如果相等,則會執行下一個比較。

  • 如果 執行下一個八 Byte 個值的位元組位元組比較。 當遇到第一個不相等配對時,它會傳回結果。 否則,它會傳回 0,表示兩 Guid 個值相等。

如果兩個 GUID 的元件值相等,則方法會比較下一個元件。 當它找到值不相等的元件時,它會傳回結果。

請注意,最後八個位元組會以反向順序出現在 的 Guid 字串表示中,從低位元組到高位元組。 例如,在值 「01e75c83-c6f5-4192-b57e-7427cec5560d」 的字串表示 Guid 中,最後八個位元組是 「b57e-7427cec5560d」。

適用於