Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the C# compiler think I'm trying to use the sbyte overload when using nullable longs?

Tags:

c#

overloading

The following code

long? long1 = 1;
long? long2 = 2;
Math.Min(long1.Value, long2);

will result in the following error message on both arguments:

  • "Argument 1: cannot convert from 'long' to 'sbyte'"
  • "Argument 2: cannot convert from 'long?' to 'sbyte'"

I understand why this is an error, I know how to resolve the error.

My question is why is the compiler trying to choose the Math.Min(sbyte, sbyte) overload in this case (even when the first argument is not a nullable)?

like image 650
Sam Arustamyan Avatar asked Oct 27 '25 00:10

Sam Arustamyan


1 Answers

It can't find the right overload, so it defaults to the first one in Math.cs, which happens to be public static sbyte Min(sbyte val1, sbyte val2)

like image 143
Yuriy Faktorovich Avatar answered Oct 29 '25 13:10

Yuriy Faktorovich