c# – 嵌套泛型函数的类型推断

前端之家收集整理的这篇文章主要介绍了c# – 嵌套泛型函数的类型推断前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经搜索了一下类型推断,但我似乎无法将任何解决方案应用于我的特定问题.

我在构建和传递函数方面做了很多工作.在我看来,它应该能够推断出int类型.我唯一能想到的是lambda返回类型不是由类型推断算法检查的.我已经删除了不必要的逻辑以更清楚地显示问题.

Func<T> Test<T>(Func<Func<T>> func)
{
    return func();
}

这编译:

Func<int> x = Test<int>(() =>
    {
        int i = 0;
        return () => i;
    });

但这会给出错误“无法根据用法推断出方法的类型参数.请尝试明确指定类型参数”:

Func<int> x = Test(() =>
    {
        int i = 0;
        return () => i;
    });

我想我只想知道为什么它以这种方式工作和任何变通方法.

解决方法

我想说,问题的正确答案是由E.Lippert在 SO Why can’t an anonymous method be assigned to var?中给出的

但是让我们一点点玩你的例子:

Func<Func<int>> f = () =>
{
    int i = 0;
    return () => i;
};

Func<int> x = Test(f); //it compiles OK

使用您的Func< T>进行类型推断没有问题.这里测试< T>(Func< Func< T> func).
隐藏的问题是您使用匿名lambda表达式,其类型无法推断.试试这个:

var f = () =>
{
    int i = 0;
    return () => i;
};

它给了@L_502_1@,说

Cannot assign lambda expression to an implicitly-typed local variable

而解释是:

An expression that is used as the initializer for an implicitly typed
variable must have a type. Because anonymous function expressions,
method group expressions,and the null literal expression do not have
a type,they are not appropriate initializers. An implicitly typed
variable cannot be initialized with a null value in its declaration,
although it can later be assigned a value of null.

现在让我们尝试另一件事:

var x = Test(() =>
{
    Func<int> f = () => 0;
    return f;
});

它编译也是如此.所以原始示例的问题实际上就是这一行:

return () => i;

我们可以更进一步,根据Eric Lippert在他的回答中提到的另一个功能来包装:

static Func<T> GetFunc<T>(Func<T> f) { return f; }

现在我们可以重写你的代码,如:

var x = Test(() =>
{
    int i = 0;
    return GetFunc(() => i);
});

它也有效.

但是,据我所知,这一切都是开销,你应该只提供一个显式类型.虽然这些变通办法是合适的,但是当你需要一个lambda时,返回一个匿名类型的对象.

猜你在找的C#相关文章