Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
CompareTo Method (Object)
Collapse the table of content
Expand the table of content

Guid.CompareTo Method (Object)

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

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

Public Function CompareTo ( _
	value As Object _
) As Integer


Type: System.Object

An object to compare, or Nothing.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and value.

Return value


A negative integer

This instance is less than value.


This instance is equal to value.

A positive integer

This instance is greater than value, or value is Nothing.




value is not a Guid.

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

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."

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.

Imports System.Runtime.InteropServices

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, 
                               guidValue }
      For Each value In values
            Console.WriteLine("{0} and {1}: {2}", guidValue, 
                              If(value Is Nothing, "null", value),
         Catch e As ArgumentException
            Console.WriteLine("Cannot compare {0} and {1}", guidValue,
                              If(value Is Nothing, "null", value))
         End Try                      
   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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
© 2015 Microsoft