C# Generic method type argument inference

Posted by CaptainCasey on Stack Overflow See other posts from Stack Overflow or by CaptainCasey
Published on 2010-05-13T06:45:18Z Indexed on 2010/05/13 6:54 UTC
Read the original article Hit count: 232

Filed under:
|

Is there any way that I can generalise the type definitions here? Ideally, I'd like to be able to change the type of 'testInput' and have test correctly infer the type at compile time.

public static void Run()
{
    var testInput = 3;
    var test = ((Func<int, int>) Identity).Compose<int,int,int>(n => n)(testInput);
    Console.WriteLine(test);
}

public static Func<T, V> Compose<T, U, V>(this Func<U, V> f, Func<T, U> g)
{
    return x => f(g(x));
}

public static T Identity<T> (this T value)
{
    return value;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about type-inference