views:

44

answers:

1

Possible Duplicate:
Why can't the C# constructor infer type?

Why is the following true:

var foo = new KeyValuePair(3,4); //doesn't compile!
var boo = new KeyValuePair<int,int>(3,4); //works fine!

I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation?

+1  A: 

Simply put, type inference only works on methods, not constructors. The reason for this is simple, constructors do not take type arguments, only types and methods do. To wit, KeyValuePair is an undefined type. Remember, it is possible, for example, to have the following types: Action, Action<T>, Action<T1, T2>, etc.

Kirk Woll