0

我可以想象这将是多个重载的问题,但是(除了 Linq)代码的很大一部分可能只有一个重载。

当只有一个过载时,它可以节省额外的铸造样板,避免类似Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.情况下的错误。

当然,您可以争辩说,当您在稍后阶段添加重载时,代码将再次开始触发上述编译器错误。但话又说回来 - 添加重载也可以破坏类型化行为(即多个接口)。

这只是不是优先事项(目前)还是我错过了一些概念问题?我要求这个来了解更多关于语言设计的信息。

static int MapLolCats(int amountOfLolcats, Func<dynamic,int> mappingFunction)
{
    return mappingFunction(amountOfLolcats);
}

// This works as expected
var amountOfLegs = MapLolCats(5, x => x * 4);

// This creates a compilation error (despite you could deduct what is meant, because there is only one overload)
// Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
dynamic jsonValue = 5;
var amountOfLegs = MapLolCats(jsonValue, x => x * 4);
4

0 回答 0