вторник, 18 ноября 2008 г.

Странности в C# Type Inference (вывод типов)

Код:

namespace test

{

    class MainClass

    {

        delegate void Del(int x);

        static void temp(int x) { Console.WriteLine(x); }

        static void test_type_inference_action<V>(Action<V> f, V p) { f(p); }

        static void test_type_inference_delegate(Del f, int p) { f(p); }

        static void Main()

        {

            test_type_inference_action(delegate(int x) { Console.WriteLine(x); }, 1);

            test_type_inference_action(x => Console.WriteLine(x), 1);

            test_type_inference_action(temp, 1);

 

            Del d = delegate(int x) { Console.WriteLine(x); };

            //ERROR

            //The type arguments for method

            //'test.MainClass.test_type_inference_action<V>(System.Action<V>, V)'

            //cannot be inferred from the usage.

            //Try specifying the type arguments explicitly.   

            test_type_inference_action(d, 1);

 

            test_type_inference_delegate(d, 1);

            test_type_inference_delegate(x => Console.WriteLine(x), 1);

            test_type_inference_delegate(temp, 1);

            test_type_inference_delegate(delegate(int x) { Console.WriteLine(x); }, 1);

        }

    }   

}



Почему в отмеченной функции выдается ошибка я понять не могу...это к вопросу о разнице между делегатами и нормальными функциональными типами.

Комментариев нет: