Output Type Explanation

I have the following code snippet:

namespace ConsoleApplication1
{
    using System.Collections.Generic;
    using System.Linq;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var bar = new object();

            var result = new int[] { 1, 2, 3 }
                .Select/* <int,int> */(bar.Test<int>)
                .ToList();
        }
    }

    public static class Extensions
    {
        public static TReturnType Test<TReturnType>(this object o, int e)
        {
            return default(TReturnType);
        }
    }
}

Compiling this on a machine with only Visual Studio 2012 works like a charm. However, to compile it on a machine in just 2010, you need to delete the comment around <int, int>.

Can someone clarify why this works in 2012, and where does the specification explain it?

+4
source share
1 answer

The problem arises from the output of the extension method type in VS2010.

If you replace the extension method with a static method, enter the output in the order:

namespace ConsoleApplication1
{
    using System.Collections.Generic;
    using System.Linq;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var result = new int[] { 1, 2, 3 }
                .Select/* <int,int> */(Extensions.Test<int>)
                .ToList();
        }
    }

    public static class Extensions
    {
        public static TReturnType Test<TReturnType>(int e)
        {
            return default(TReturnType);
        }
    }
}

There is no clear answer from Microsoft to this question in C # Language Specification version 5.0 (see section 7.5.2).

: why-doesnt-this-code-compile-in-vs2010-with-net-4-0

+2

All Articles