แก้ไข

แชร์ผ่าน


Guid.CompareTo Method

Definition

Compares this instance to a specified object or Guid and returns an indication of their relative values.

Overloads

CompareTo(Guid)

Compares this instance to a specified Guid object and returns an indication of their relative values.

CompareTo(Object)

Compares this instance to a specified object and returns an indication of their relative values.

CompareTo(Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Compares this instance to a specified Guid object and returns an indication of their relative values.

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

Parameters

value
Guid

An object to compare to this instance.

Returns

A signed number indicating the relative values of this instance and value.

Return value Description
A negative integer This instance is less than value.
Zero This instance is equal to value.
A positive integer This instance is greater than value.

Implements

Examples

The following example calls the CompareTo(Guid) method to compare a GUID value with two similar GUID values.

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

Remarks

The CompareTo method compares the GUIDs as if they were values provided to the Guid(Int32, Int16, Int16, Byte[]) constructor, as follows:

  • It compares the UInt32 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • It compares the first UInt16 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • It compares the second UInt16 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • If performs a byte-by-byte comparison of the next eight Byte values. When it encounters the first unequal pair, it returns the result. Otherwise, it returns 0 to indicate that the two Guid values are equal.

Note that the final eight bytes appear in the string representation of a Guid in reverse order, from low byte to high byte. For example, in the string representation of the Guid value "01e75c83-c6f5-4192-b57e-7427cec5560d", the final eight bytes are "b57e-7427cec5560d." In other words, the final eight bytes are compared on a byte-by-byte basis from left to right starting with 0xb5.

If two GUIDs have equal values for a component, the method compares the next component. When it finds a component whose values are unequal, it returns the result.

This method implements the System.IComparable<T> interface and performs slightly better than the Guid.CompareTo method because it does not have to convert the value parameter to a Guid value.

Applies to

CompareTo(Object)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Compares this instance to a specified object and returns an indication of their relative values.

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

Parameters

value
Object

An object to compare, or null.

Returns

A signed number indicating the relative values of this instance and value.

Return value Description
A negative integer This instance is less than value.
Zero This instance is equal to value.
A positive integer This instance is greater than value, or value is null.

Implements

Exceptions

value is not a Guid.

Examples

The following example uses the GuidAttribute attribute to assign a GUID to a class. It retrieves the value of this GUID by calling the Attribute.GetCustomAttribute method and passing the Value property of the returned GuidAttribute object to the Parse method. Then it compares that GUID with an array of values.

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

Remarks

The value parameter must be null or an instance of Guid; otherwise, an exception is thrown. Any instance of Guid, regardless of its value, is considered greater than null.

The CompareTo method compares the GUIDs as if they were values provided to the Guid constructor, as follows:

  • It compares the Int32 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • It compares the first Int16 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • It compares the second Int16 values, and returns a result if they are unequal. If they are equal, it performs the next comparison.

  • If performs a byte-by-byte comparison of the next eight Byte values. When it encounters the first unequal pair, it returns the result. Otherwise, it returns 0 to indicate that the two Guid values are equal.

If two GUIDs have equal values for a component, the method compares the next component. When it finds a component whose values are unequal, it returns the result.

Note that the final eight bytes appear in the string representation of a Guid in reverse order, from low byte to high byte. For example, in the string representation of the Guid value "01e75c83-c6f5-4192-b57e-7427cec5560d", the final eight bytes are "b57e-7427cec5560d."

Applies to