Func <>参数的差异

Osc*_*ros 3 c# generics lambda variance c#-4.0

我一直在尝试做类似的事情

Func<string, bool> f
Func<object, bool> F = f;
Run Code Online (Sandbox Code Playgroud)

并且编译器引发以下错误:

无法隐式转换'System.Func<string,bool>''System.Func<object,bool>'

但是,我们显然可以做到 object x = "123"

p.s*_*w.g 5

想象一下,如果你这样做了:

Func<string, bool> fStr = str => str.Length > 10;
Func<object, bool> fObj = fStr;
Run Code Online (Sandbox Code Playgroud)

那么,根据签名fObj,您应该可以使用以下任何参数调用它:

fObj(7);
Run Code Online (Sandbox Code Playgroud)

这显然是无效的fStr.