Resolving the wrong function at compile time BUG?

I am using Visual Studio 2012 Update 1 and .NET 4.5 code here.

void Test(Action a) { }
void Test(Func<int> a) { }
void TestError()
{
    bool throwException = true;
    //Resolves to Test(Action a)
    Test(() =>
    {
    });
    //Resolves to Test(Action a)
    Test(() =>
    {
        if (throwException) throw new Exception();
    });
    //Resolves to Test(Func<int> a)
    //(This seems like a bug since there is no return value)
    Test(() =>
    {
        throw new Exception();
    });
    //Resolves to Test(Action a)
    //(With warning unreachable code detected)
    Test(() =>
    {
        throw new Exception();
        return; //unreachable code detected
    });
}

It looks like the last function call resolves Func instead of Action incorrectly and has something to do with unconditional exception throwing.

Is this a mistake? Thank.

+5
source share
1 answer

Well, it seems reasonable to me that both are valid candidates. In other words, I believe that for the conversion of () => { throw new Exception(); }both Func<int>, it Actionmakes sense to convert () => { throw new Exception(); }. In both cases, the end of the method is unavailable - it would be fair to put it in a normal named method as follows:

public int GoBang()
{
    throw new Exception();
}

and then write:

Func<int> foo = GoBang;

, - Func<int> Action . , . 7.5.3.2 ( ), :

E x P x , E x Q < > >

7.5.3.3 ( ) :

C 1 , C 2, :

  • [...]
  • E - , T 1 - D 1 . Expression < D 1 > , T 2 D 2, Expression < D 2 > , :

    • [...]
    • D 1 D 2 , :
    • D 1 Y, D 2 void.

, Func<int> Action... .

+7

All Articles