Why does this static factory method involving implied generic types, work?

Posted by Cheeso on Stack Overflow See other posts from Stack Overflow or by Cheeso
Published on 2009-12-02T13:34:54Z Indexed on 2010/05/12 8:34 UTC
Read the original article Hit count: 132

Filed under:
|

Consider

public class Tuple<T1, T2>
{
    public Tuple(T1 v1, T2 v2)
    {
        V1 = v1;
        V2 = v2;
    }

    public T1 V1 { get; set; }
    public T2 V2 { get; set; }
}

public static class Tuple
{
    // MAGIC!!
    public static Tuple<T1, T2> New<T1, T2>(T1 v1, T2 v2)
    {
        return new Tuple<T1, T2>(v1, v2);
    }
}

Why does the part labeled "MAGIC" in the above work? It allows syntax like Tuple.New(1, "2") instead of new Tuple<int, string>(1, "2"), but ... how and why?

Why do I not need Tuple.New<int,string>(1, "2") ??

© Stack Overflow or respective owner

Related posts about generics

Related posts about c#