Type inference classes

suggest change

Developers can be caught out by the fact that type inference doesn’t work for constructors:

class Tuple<T1,T2>
{
   public Tuple(T1 value1, T2 value2)
   {
   }
}

var x = new Tuple(2, "two");              // This WON'T work...
var y = new Tuple<int, string>(2, "two"); // even though the explicit form will.

The first way of creating instance without explicitly specifying type parameters will cause compile time error which would say:

Using the generic type ‘Tuple<T1, T2>’ requires 2 type arguments

A common workaround is to add a helper method in a static class:

static class Tuple
{
    public static Tuple<T1, T2> Create<T1, T2>(T1 value1, T2 value2)
    {
         return new Tuple<T1, T2>(value1, value2);
    }
}

var x = Tuple.Create(2, "two");  // This WILL work...

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Generics:
* Type inference classes

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
57 Generics
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script