方差Func键<>参数 [英] Variance in Func<> arguments

查看:152
本文介绍了方差Func键<>参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图做这样的事情。

I've been trying to do something like

Func<string, bool> f
Func<object, bool> F = f;

和编译器引发了以下错误:

and the compiler raises the following error:

无法隐式转换类型'System.Func<字符串,布尔> 'System.Func<对象,布尔>

不过,我们显然可以做到对象x =123

However, we obviously can do object x = "123"

推荐答案

试想一下,如果你做了这一点:

Imagine if you had done this:

Func<string, bool> fStr = str => str.Length > 10;
Func<object, bool> fObj = fStr;



那么,根据 fObj 的签名,你应该能够与这样的任何参数来调用它:

Well, according to the signature of fObj, you should be able to call it with any argument like this:

fObj(7);



这显然是无效的 FSTR

这篇关于方差Func键&LT;&GT;参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆