I do not understand why the following is ambiguous according to compiler:
byte x = 200;
int novaCervena = Math.Min(x, 10);
And once I add +1 to byte it is not
byte x = 200;
int novaCervena = Math.Min(x+1, 10);
I do not understand why the following is ambiguous according to compiler:
byte x = 200;
int novaCervena = Math.Min(x, 10);
And once I add +1 to byte it is not
byte x = 200;
int novaCervena = Math.Min(x+1, 10);
It's definitely not ambiguous when you use
x+1as the type of the first argument is thenint. (There's nobyte+byteoperator in C#.)In the first case, you have a
byteargument which can be implicitly converted to anint, but then an integer literal argument. The argument is of typeint, but with an implicit constant expression conversion tobyte(see section 6.1.9). So while bothMin(byte, byte)andMin(int, int)are applicable overloads, each is "preferred" for a different parameter (due to the conversions available), hence the ambiguity.Note that if you have a "normal" expression of type
int(as opposed to a constant expression) the ambiguity goes away:Likewise a normal
byteargument:Or you can force the conversion either way: