简体   繁体   English

为什么C#编译器认为我在使用可空的long时尝试使用sbyte重载?

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

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'" “参数1:无法从'long'转换为'sbyte'”
  • "Argument 2: cannot convert from 'long?' “争论2:无法转换'长?' to 'sbyte'" ''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)? 我的问题是为什么编译器在这种情况下尝试选择Math.Min(sbyte, sbyte)重载(即使第一个参数不可为空)?

它无法找到正确的重载,因此它默认为Math.cs中的第一个,恰好是public static sbyte Min(sbyte val1, sbyte val2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C#编译器使用无效方法的重载? - Why C# compiler use an invalid method's overload? 为什么要尝试使用字符串? C#中的(Nullable string)产生语法错误? - Why does trying to use string? (Nullable string) in C# produce a syntax error? 为什么我不总是在C#中使用可为空的类型 - Why shouldn't I always use nullable types in C# 为什么C#编译器重载解析算法将具有相同签名的静态和实例成员视为相等? - Why does C# compiler overload resolution algorithm treat static and instance members with equal signature as equal? 为什么C#编译器在使用LINQ方法时会创建私有DisplayClass Any()以及如何避免它? - Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it? 当我传递HashSet时,为什么我的泛型类型方法认为T是system.object - Why does my generic type method think T is system.object when I'm passing in a HashSet 在C#/ .NET中,为什么sbyte []与byte []相同,除非它不是? - In C#/.NET why is sbyte[] the same as byte[] except that it's not? 为什么当我使用 !!= C# 时代码会被编译 - Why does the code get compiled when I use !!= C# 在C#中除以2个long时获得0值 - Getting a value of 0 when dividing 2 longs in c# 我正在尝试使用ReadLine()或Read()或ReadKey来使用C#读取整数 - I'm trying to to use ReadLine(), or Read() or ReadKey to read in integers using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM