Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不允许将此命名函数作为Func传递<&燃气轮机;C#中的参数?_C#_C# 3.0_Casting_Delegates - Fatal编程技术网

为什么不允许将此命名函数作为Func传递<&燃气轮机;C#中的参数?

为什么不允许将此命名函数作为Func传递<&燃气轮机;C#中的参数?,c#,c#-3.0,casting,delegates,C#,C# 3.0,Casting,Delegates,我很难理解C#3.0中通过代理的规则是什么。最后一个.Max()给出了如下所示的编译错误,但我无法理解这种情况与其他任何情况之间的显著区别 int NamedIdentity(int val) { return val; } Func<int, int> Identity = x => x; void Main() { var range = Enumerable.Range(1, 10); range.Max(); range.Max(x =>

我很难理解C#3.0中通过代理的规则是什么。最后一个
.Max()
给出了如下所示的编译错误,但我无法理解这种情况与其他任何情况之间的显著区别

int NamedIdentity(int val) { return val; }
Func<int, int> Identity = x => x;

void Main() {
    var range = Enumerable.Range(1, 10);

    range.Max();
    range.Max(x => x);
    range.Max(Identity);
    range.Max((Func<int, int>) NamedIdentity);
    range.Max(new Func<int, int>(NamedIdentity));

    // this line gives compile error
    range.Max(NamedIdentity); 
    /* The call is ambiguous between the following methods or properties: 
    * 'System.Linq.Enumerable.Max<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal>)' and 
    * 'System.Linq.Enumerable.Max<int>(System.Collections.Generic.IEnumerable<int>, System.Func<int,decimal?>)'
    */
}
intnamedidentity(intval){returnval;}
Func Identity=x=>x;
void Main(){
var范围=可枚举范围(1,10);
range.Max();
range.Max(x=>x);
range.Max(标识);
range.Max((Func)NamedIdentity);
range.Max(新函数(NamedIdentity));
//这一行给出了编译错误
range.Max(NamedIdentity);
/*以下方法或属性之间的调用不明确:
*'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable,System.Func)'和
*'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable,System.Func)'
*/
}
为什么不编译?我的理解是,
NamedIdentity
被声明为
Func
,因此它不需要强制转换,尽管我显然错了。一个线索是编译错误是关于
Decimal
s的,尽管代码中没有引用任何一个。那是从哪里来的?函数引用不能隐式转换为具有相同签名的委托吗?



这在C#4.0中得到了修复。

@recursive:我相信,因为这是前两个重载。我不完全确定。