<p>C# doesn't require you to specify a generic type parameter if the compiler can infer it, for instance:</p>

<pre><code>List&lt;int&gt; myInts = new List&lt;int&gt; {0,1,1,
    2,3,5,8,13,21,34,55,89,144,233,377,
    610,987,1597,2584,4181,6765};

//this statement is clunky
List&lt;string&gt; myStrings = myInts.
    Select&lt;int,string&gt;( i =&gt; i.ToString() ).
    ToList&lt;string&gt;();

//the type is inferred from the lambda expression
//the compiler knows that it's taking an int and 
//returning a string
List&lt;string&gt; myStrings = myInts.
    Select( i =&gt; i.ToString() ).
    ToList();
</code></pre>

<p>This is needed for anonymous types where you don't know what the type parameter would be (in intellisense it shows up as <code>'a</code>) because it's added by the compiler.</p>

<p>Class-level type parameters don't let you do this:</p>

<pre><code>//sample generic class
public class GenericDemo&lt;T&gt; 
{
    public GenericDemo ( T value ) 
    {
        GenericTypedProperty = value;
    }

    public T GenericTypedProperty {get; set;}
}

//why can't I do:
int anIntValue = 4181;
var item = new GenericDemo( anIntValue ); //type inference fails

//however I can create a wrapper like this:
public static GenericDemo&lt;T&gt; Create&lt;T&gt; ( T value )
{
    return new GenericDemo&lt;T&gt; ( value );
}

//then this works - type inference on the method compiles
var item = Create( anIntValue );
</code></pre>

<p>Why doesn't C# support this class level generic type inference?</p>

[{'answer_id': 45612, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>Why doesn't C# support this class level generic type inference?</p>\n</blockquote>\n\n<p>Because they're generally ambiguous. By contrast, type inference is trivial for function calls (if all types appear in arguments). But in the case of constructor calls (glorified functions, for the sake of discussion), the compiler has to resolve multiple levels at the same time. One level is the class level and the other is the constructor arguments level. I believe solving this is algorithmically non-trivial. Intuitively, I'd say it's even NP-complete.</p>\n\n<p>To illustrate an extreme case where resolution is impossible, imagine the following class and tell me what the compiler should do:</p>\n\n<pre><code>class Foo&lt;T&gt; {\n    public Foo&lt;U&gt;(U x) { }\n}\n\nvar x = new Foo(1);\n</code></pre>\n"}, {'answer_id': 45706, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>Thanks Konrad, that's a good response (+1), but just to expand on it.</p>\n\n<p>Let's pretend that C# has an explicit constructor function:</p>\n\n<pre><code>//your example\nvar x = new Foo( 1 );\n\n//becomes\nvar x = Foo.ctor( 1 );\n\n//your problem is valid because this would be\nvar x = Foo&lt;T&gt;.ctor&lt;int&gt;( 1 );\n//and T can't be inferred\n</code></pre>\n\n<p>You're quite right that the first constructor can't be inferred.</p>\n\n<p>Now let's go back to the class</p>\n\n<pre><code>class Foo&lt;T&gt; \n{\n    //&lt;T&gt; can't mean anything else in this context\n    public Foo(T x) { }\n}\n\n//this would now throw an exception unless the\n//typeparam matches the parameter\nvar x = Foo&lt;int&gt;.ctor( 1 );\n\n//so why wouldn't this work?\nvar x = Foo.ctor( 1 );\n</code></pre>\n\n<p>Of course, if I add your constructor back in (with its alternate type) we have an ambiguous call - exactly as if a normal method overload couldn't be resolved.</p>\n"}, {'answer_id': 45728, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': "<p>Actually, your question isn't bad. I've been toying with a generic programming language for last few years and although I've never come around to actually develop it (and probably never will), I've thought a lot about generic type inference and one of my top priorities has always been to allow the construction of classes without having to specify the generic type.</p>\n\n<p>C# simply lacks the set of rules to make this possible. I think the developers never saw the neccesity to include this. Actually, the following code would be very near to your proposition and solve the problem. All C# needs is an added syntax support.</p>\n\n<pre><code>class Foo&lt;T&gt; {\n    public Foo(T x) { … }\n}\n\n// Notice: non-generic class overload. Possible in C#!\nclass Foo {\n    public static Foo&lt;T&gt; ctor&lt;T&gt;(T x) { return new Foo&lt;T&gt;(x); }\n}\n\nvar x = Foo.ctor(42);\n</code></pre>\n\n<p>Since this code actually works, we've shown that the problem is not one of semantics but simply one of lacking support. I guess I have to take back my previous posting. ;-)</p>\n"}]
['https://Stackoverflow.com/questions/45604', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/905/']