小编dea*_*ull的帖子

使用Action <T>参数重载调用不明确的方法

我在调用具有不同Action<T>变体的重载方法时遇到了一些意外的编译器行为.

假设我有这个类Test,我在CallTest构造函数中创建它的实例.

public class Test
{
    public Test(Action<long> arg)
    {

    }

    public Test(Action<decimal> arg)
    {

    }
}

public class CallTest
{
    public CallTest()
    {
        Test t = new Test(TestDecimal);
    }

    public void TestDecimal(decimal arg)
    {

    }

    public void TestLong(long arg)
    {

    }    
}
Run Code Online (Sandbox Code Playgroud)

Test使用TestDecimalTestLong作为参数调用构造函数时,我收到以下错误:

以下方法或属性之间的调用不明确:' Test(System.Action<long>)'和' Test(System.Action<decimal>)'

我的猜测是有一些隐式转换之间正在进行的longdecimal,但没有任何人有任何其他想法可能我做错?有没有解决方法?

c# ambiguity overload-resolution

6
推荐指数
1
解决办法
817
查看次数

标签 统计

ambiguity ×1

c# ×1

overload-resolution ×1