c# - Why can I compare sbyte to all the other numeric types *except* ulong? -


you can >, <, ==, etc. comparisons between sbyte , byte, int, uint, short, ushort, long, double, , float. not ulong.

my brain exploding. can explain why sbyte can compared uint not ulong?

public bool sbyte_ulong_compare(sbyte x, ulong y) {     return x < y;  // compiler error cs0019 } 

also, using unchecked doesn't make things work better. brain melting.

another edit. works:

public bool sbyte_ulong_compare(sbyte x, ulong y) {        //     // returns x < y     //     if (x < 0)         return true;      if (y > 127)         return true;      return ((long)x < (long)y); } 

dthorpe , jon's answers close not quite correct.

the correct reasoning follows.

the specification states:

for operation of form x op y, op comparison operator, overload resolution applied select specific operator implementation.

ok, operator implementations overload resolution has work with? are:

bool operator <(int x, int y); bool operator <(uint x, uint y); bool operator <(long x, long y); bool operator <(ulong x, ulong y); bool operator <(float x, float y); bool operator <(double x, double y); bool operator <(decimal x, decimal y); 

plus enum less-than operator enumerated types, plus lifted-to-nullable versions of each of foregoing.

overload resolution must first eliminate inapplicable operators, , remaining set of applicable operators, determine best operator.

the int, uint, long , enum operators (and lifted forms) eliminated because ulong not implicitly convert types.

the uint , ulong operators (and lifted forms) eliminated because sbyte not implicitly convert types.

that leaves

bool operator <(float x, float y); bool operator <(double x, double y); bool operator <(decimal x, decimal y); 

and lifted forms. must determine best operator six.

what mean "best"? when comparing 2 operators, 1 more specific operand types better one. "more specific" mean "tiger" more specific "animal" because tigers convertible animal not animals convertible tiger.

clearly unlifted forms better of corresponding lifted forms. non-nullable type more specific corresponding nullable type because non-nullable type convertible nullable type, not vice-versa. can eliminate lifted forms.

that leaves three. of 3 best?

float more specific double. every float convertible double, not every double convertible float. therefore double eliminated. leaves two.

bool operator <(float x, float y); bool operator <(decimal x, decimal y); 

which of these best? there no implicit conversion float decimal. there no implicit conversion decimal float. therefore neither better other.

therefore no best operator can determined. overload resolution fails.

we have decided report generic error message says there no such operator want, rather giving seemingly bizarre , confusing error message "operator overload resolution failed because float neither better nor worse decimal". think reasonable design choice.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -