【发布时间】:2013-09-26 23:24:21
【问题描述】:
所以说我有一个重载的方法。一个接受一个返回 uint 的函数,一个接受一个返回 int 的函数。
static void test1(Func<uint> f)
{
//things
}
static void test1(Func<int> f)
{
// also things
}
现在我试着这样称呼它:
test1(Random.FunctionThatReturnsUints);
但我在编译时得到一个模棱两可的调用错误:
Error 4 The call is ambiguous between the following methods or
properties: 'RNG_Comparison.Run.test1(System.Func<int>)' and 'RNG_Comparison.Run.test1
(System.Func<uint>)'
那是怎么回事?重载函数的全部意义不在于它基于类型隐含地理解您的意思吗?我的意思是,如果我用返回 BigInt 之类的 func 来调用它,也许我会看到编译器的困惑,但是这个看起来很简单。
有人知道我为什么会收到这个错误吗?
【问题讨论】:
-
尝试
test1(new Func<uint>(Random.FunctionThatReturnsUints))看看是否会导致不同的编译器错误。 -
@cdhowie,这将解决编译器错误。
-
重载似乎没问题,请发布更多代码。
-
查看 Eric Lippert 关于类似主题的回答 stackoverflow.com/a/2058854/961113
标签: c# function-pointers