Export (0) Print
Expand All

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 int CompareTo(
	Guid 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


A negative integer

This instance is less than value.


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

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

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.

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, 
                               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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft