IComparable(T) Interface
Collapse the table of content
Expand the table of content

IComparable<T> Interface

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method for ordering instances.

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

public interface IComparable<in T>

Type Parameters

in T

The type of objects to compare.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see [2678dc63-c7f9-4590-9ddc-0a4df684d42e].

The IComparable<T> type exposes the following members.

Public methodCompareToCompares the current object with another object of the same type.

This interface is implemented by types whose values can be ordered; for example, the numeric and string classes. A value type or class implements the CompareTo method to create a type-specific comparison method suitable for purposes such as sorting.


The IComparable<T> interface defines the CompareTo method, which determines the sort order of instances of the implementing type. The IEquatable<T> interface defines the Equals method, which determines the equality of instances of the implementing type.

The IComparable<T> interface provides a strongly typed comparison method for ordering members of a generic collection object. Because of this, it is usually not called directly from developer code. Instead, it is called automatically by sorting methods such as the List<T>.Sort() method.

Notes to Implementers

Replace the type parameter of the IComparable<T> interface with the type that is implementing this interface.

The following code example illustrates the implementation of IComparable for a simple Temperature object.

using System;
using System.Collections.Generic;

public class Address : IComparable<Address>
   private string addr1, addr2, cty, st, coun, postal;

   public Address(string address1, string address2, string city, 
                  string state, string postalCode, string country)
      this.addr1 = address1;
      this.addr2 = address2;
      this.cty = city;
      this.st = state;
      this.coun = country;
      this.postal = postalCode;      

   public string Address1
      get { return this.addr1; }
      set { this.addr1 = value; }

   public string Address2
      get {return this.addr2; }
      set { this.addr2 = value; }

   public string City
      get { return this.cty; }
      set { this.cty = value; }

   public string State
      get { return this.st; }
      set { this.st = value; }

   public string Country
      get { return this.coun; }
      set { this.coun = value; }

   public string PostalCode
      get {return this.postal; }
      set {this.postal = value; }

   public override string ToString()
      string addressLine2 = string.IsNullOrEmpty(addr2) ? "" : addr2 + "\n";
      return addr1 + "\n" + addressLine2 +              
             cty + ", " + st + " " + postal + " " + coun;

   public int CompareTo(Address other)
      // If other is null, this instance is greater.
      if (other == null) return 1;

      string otherAddress = other.Country + other.State + other.PostalCode +
                                   other.City + other.Address1 + other.Address2;
      return (coun + st + postal + cty + addr1 + addr2).CompareTo(otherAddress);

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      List<Address> addresses = new List<Address>();
      addresses.Add(new Address("106 East 5th St.", "", "New City", "MI", "48002", "USA"));
      addresses.Add(new Address("47 East End Rd.", "", "Huxenplux", "NJ", "20203", "USA"));
      addresses.Add(new Address("12043 N.E. 72nd St.", "", "Belleville", "WA", "98101", "USA"));
      foreach (Address address in addresses)
         outputBlock.Text += address.ToString() + "\n\n";


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2016 Microsoft