What are some advantages & disadvantages of type inference in C#?

Type inference was invented for exactly the reason you give for C++, you can create anonymous types that don't HAVE a type name (see Lambdas and Linq in particular).

So in that case it's needed.

In the other case (when the type name is known) then it comes down to style. I use var when the type is really obvious:

// I like this - less duplication and easier to read
var item = new List<ComplexObjectItem>();

instead of:

List<ComplexObjectItem> item = new List<ComplexObjectItem>();

Because it reduces duplication.

However, I prefer not to use it when the type is not immediately obvious to the reader:

// I don't like this - I need to look up what the type is
var item = ResultOfSomeFunctionWhereICantSeeWhatItIs();

But your mileage might vary.


I think common sense dictates the following informal rules:

If there's some long name such as :

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>> myVar = new Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>>();

then replacing it with

var myVar = new Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>>();

makes sense because you can still tell what the object is.

Something ambiguous, on the other hand, might warrant not using var:

Lazy<List<MyNamespace.ISomeVeryLongInterfaceType>> myVar = doProcess();