Guid.CompareTo Method (Object)

July 28, 2014

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

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

public int CompareTo(
	Object value


Type: System.Object
An object to compare, or null.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and 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.


value is null.




value is not a Guid.

Any instance of Guid, regardless of its value, is considered greater than null.

value must be null or an instance of Guid; otherwise, an exception is thrown.

The following code sample demonstrates how to attach and read a Guid object as an attribute on a user-defined class or interface.

using System;
using System.Runtime.InteropServices;

// Guid for the interface IMyInterface.
interface IMyInterface
   void MyMethod();

// Guid for the coclass MyTestClass.
public class Example : IMyInterface
   // Run regasm on this assembly to create .reg and .tlb files.
   // Reg file can be used to register this coclass in the registry.
   // Tlb file will be used to do interop.

   public void MyMethod() { }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Example addresses the following in System.Runtime.InterOpServices.GuidAttribute.
      // How to specify the attribute on interface/coclass.
      // Retrieve the GuidAttribute from an interface/coclass.
      // Value property on GuidAttribute class.

      // Example addresses the following in System.Guid.
      // Constructor Guid(string).
      // Constructor Guid(ByteArray).
      // Equals.
      // Operator ==.
      // CompareTo.

      Attribute IMyInterfaceAttribute = Attribute.GetCustomAttribute(typeof(IMyInterface), typeof(GuidAttribute));

      // The Value property of GuidAttribute returns a string. 
      outputBlock.Text += "IMyInterface Attribute: " + ((GuidAttribute)IMyInterfaceAttribute).Value + "\n";

      // Using the string to create a guid.
      Guid myGuid1 = new Guid(((GuidAttribute)IMyInterfaceAttribute).Value);
      // Using a byte array to create a guid.
      Guid myGuid2 = new Guid(myGuid1.ToByteArray());

      // Equals is overridden and so value comparison is done though references are different.
      if (myGuid1.Equals(myGuid2))
         outputBlock.Text += "myGuid1 equals myGuid2" + "\n";
         outputBlock.Text += "myGuid1 not equals myGuid2" + "\n";

      // Equality operator can also be used to determine if two guids have same value.
      if (myGuid1 == myGuid2)
         outputBlock.Text += "myGuid1 == myGuid2" + "\n";
         outputBlock.Text += "myGuid1 != myGuid2" + "\n";

      // CompareTo returns 0 if the guids have same value.
      if (myGuid1.CompareTo(myGuid2) == 0)
         outputBlock.Text += "myGuid1 compares to myGuid2" + "\n";
         outputBlock.Text += "myGuid1 does not compare to myGuid2" + "\n";


      //IMyInterface Attribute: F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
      //myGuid1 equals myGuid2
      //myGuid1 == myGuid2
      //myGuid1 compares to myGuid2

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2014 Microsoft