By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,719 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

Re: Confused by the CompareTo method of IComparable

P: n/a
Author <gn********@gmail.comwrote:
I understand that if we wanna compare two objects of the same class,
we need to implement the IComparable interface, as shown here:

http://msdn.microsoft.com/en-us/libr...(printer).aspx

I am confused by the implementation of the CompareTo method in the C#
example on the above page. Look:

public int CompareTo(object obj)
{
if(obj is Temperature)
{
Temperature otherTemperature = (Temperature) obj;
return
this.temperatureF.CompareTo(otherTemperature.tempe ratureF);
}
else
{
throw new ArgumentException("object is not a Temperature");
}
}

The thing that baffles me is that the implementation of CompareTo
makes a call to this method itself.
Where? It looks to me like it's calling double's implementation of
CompareTo.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jul 10, 3:08*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
I understand that if we wanna compare two objects of the same class,
we need to implement the IComparable interface, as shown here:
http://msdn.microsoft.com/en-us/libr...ble.compareto(...
I am confused by the implementation of the CompareTo method in the C#
example on the above page. *Look:
public int CompareTo(object obj)
{
* * * * if(obj is Temperature)
* * * * {
* * * * * * Temperature otherTemperature = (Temperature) obj;
* * * * * * return
this.temperatureF.CompareTo(otherTemperature.tempe ratureF);
* * * * }
* * * * else
* * * * {
* * * * * *throw new ArgumentException("object is not a Temperature");
* * * * }
}
The thing that baffles me is that the implementation of CompareTo
makes a call to this method itself.

Where? It looks to me like it's calling double's implementation of
CompareTo.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com
That's right. So, double actually implements CompareTo like what I
did. That must be it. Thank you.
Jul 10 '08 #2

P: n/a
On Jul 10, 3:08*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
I understand that if we wanna compare two objects of the same class,
we need to implement the IComparable interface, as shown here:
http://msdn.microsoft.com/en-us/libr...ble.compareto(...
I am confused by the implementation of the CompareTo method in the C#
example on the above page. *Look:
public int CompareTo(object obj)
{
* * * * if(obj is Temperature)
* * * * {
* * * * * * Temperature otherTemperature = (Temperature) obj;
* * * * * * return
this.temperatureF.CompareTo(otherTemperature.tempe ratureF);
* * * * }
* * * * else
* * * * {
* * * * * *throw new ArgumentException("object is not a Temperature");
* * * * }
}
The thing that baffles me is that the implementation of CompareTo
makes a call to this method itself.

Where? It looks to me like it's calling double's implementation of
CompareTo.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com
I had thought it was this.CompareTo(that.temperature).
Jul 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.