Guid.CompareTo Method (Guid)

 

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Function CompareTo (
	value As Guid
) As Integer

Parameters

value
Type: System.Guid

An object to compare to this instance.

Return Value

Type: System.Int32

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.

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(Of 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.

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

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: