Pages

Sunday, 5 October 2008

CompareTo method

CompareTo() is very easy to understand but I just seem to have a blind spot about it.

int result = instance.CompareTo(obj);

ResultMeaning
Less than zeroinstance is less than obj
Zeroinstance is equal to obj
Greater than zeroinstance is greater than obj

For thisUse this
if (instance < obj)if (instance.CompareTo(obj) < 0)
if (instance <= obj)if (instance.CompareTo(obj) <= 0)
if (instance == obj)if (instance.CompareTo(obj) == 0)
if (instance > obj)if (instance.CompareTo(obj) > 0)
if (instance >= obj)if (instance.CompareTo(obj) >= 0)

No comments: