Tag: 方法组

C#语言设计:`is`操作符内的方法组

我对C#语言的一些设计选择感兴趣。 C#规范中有一条规则允许使用方法组作为运算符的表达式: class Foo { static void Main() { if (Main is Foo) Main(); } } 上述条件总是错误的,因为规范说: 7.10.10是运营商 • 如果E是方法组或空文字,如果E的类型是引用类型或可空类型且E的值为null,则结果为false。 我的问题:允许在CLR中使用没有运行时表示的C#语言元素的目的/点/原因是什么,就像这样的“运行时”运算符中的方法组一样?

方法推理不适用于方法组

考虑 void Main() { var list = new[] {“1”, “2”, “3”}; list.Sum(GetValue); //error CS0121 list.Sum(s => GetValue(s)); //works ! } double GetValue(string s) { double val; double.TryParse(s, out val); return val; } CS0121错误的描述是 以下方法或属性之间的调用不明确: ‘System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func)’和’System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func )’ 我不明白的是,什么信息s => GetValue(s)给编译器简单的GetValue不 – 对于前者不是后者的语法糖?