Brian Richards <(E-Mail Removed)> wrote:
> This code throws an exception:
>
> Double foo = 1.0;
> Console.WriteLine(((IComparable)foo).CompareTo(1));
>
> But this does not:
>
> Double foo = 1.0;
> Console.WriteLine(foo.CompareTo(1));
>
>
> Does anyone know why? I'm assuming it has to do with the way Double
> implements IComparable and it's trying to call CompareTo(double) and not
> CompareTo(object) when cast as IComparable.
No, when it's cast to IComparable, it's calling CompareTo(object),
whereas when it's not cast, the integer is being implicitly converted
to a double before calling CompareTo(double).
--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too