本文介绍了为什么在这种情况下C#无法解决正确的重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很明确的奇怪情况,但是过载解析器却不这么认为。考虑:

I've come across a strange situation which is non-ambiguous, yet the overload resolver doesn't think so. Consider:

public static class Program
{
    delegate int IntDel();
    delegate string StringDel();

    delegate void ParamIntDel(int x);
    delegate void ParamStringDel(string x);

    static void Test(IntDel fun) { }
    static void Test(StringDel fun) { }
    static void ParamTest(ParamIntDel fun) { }
    static void ParamTest(ParamStringDel fun) { }

    static int X() { return 42; }
    static void PX(int x) { }

    public static void Main(string[] args)
    {
        ParamTest(PX); // OK
        Test(X); // Ambiguos call!
    }
}

如何呼叫 ParamTest 重载可以正确解决,但是 Test 重载是模棱两可的?

How come the call to ParamTest overloads is resolved correctly, but Test overload is ambiguous?

推荐答案

也许是因为

IntDel StringDel 之间的唯一区别是返回值。

And the only difference between IntDel and StringDel is in the return value.

更具体地说:

这篇关于为什么在这种情况下C#无法解决正确的重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 04:56