Export (0) Print
Expand All
9 out of 11 rated this helpful - Rate this topic

IComparable.CompareTo Method

Compares the current instance with another object of the same type.

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

int CompareTo (
	Object obj
)
int CompareTo (
	Object obj
)
function CompareTo (
	obj : Object
) : int
Not applicable.

Parameters

obj

An object to compare with this instance.

Return Value

A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:

Value

Meaning

Less than zero

This instance is less than obj.

Zero

This instance is equal to obj.

Greater than zero

This instance is greater than obj.

Exception typeCondition

ArgumentException

obj is not the same type as this instance.

This method is only a definition and must be implemented by a specific class or value type to have effect. The meaning of the comparisons, "less than," "equal to," and "greater than," depends on the particular implementation.

By definition, any object compares greater than a null reference (Nothing in Visual Basic), and two null references compare equal to each other.

The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown.

Notes to Implementers: For objects A, B and C, the following must be true: A.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero, then B.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero, then A.CompareTo(C) is required to return zero. If A.CompareTo(B) returns a value other than zero, then B.CompareTo(A) is required to return a value of the opposite sign. If A.CompareTo(B) returns a value x not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to return a value of the same sign as x and y.

Notes to Callers: Use the CompareTo method to determine the ordering of instances of a class.

The following code sample illustrates the use of CompareTo to compare a Temperature object implementing IComparable with another object. The Temperature object implements CompareTo by simply wrapping a call to the CompareTo method.

using System;

namespace Snippets {
    public class Temperature : IComparable {
        /// <summary>
        /// IComparable.CompareTo implementation.
        /// </summary>

        // The value holder
        protected double m_value;

        public int CompareTo(object obj) {
            if(obj is Temperature) {
                Temperature temp = (Temperature) obj;
                return m_value.CompareTo(temp.m_value);
            }
            throw new ArgumentException("object is not a Temperature");    
        }

        public double Value {
            get {
                return m_value;
            }
            set {
                m_value = value;
            }
        }

        public double Celsius {
            get {
                return (m_value - 32) / 1.8;
            }
            set {
                m_value = (value * 1.8) + 32;
            }
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.