Var keyword type inference ambiguity when both interface and implementation are present

c#

Solution

You are not actually "programming to interfaces" if you are still instantiating the concrete class within the method, as the dependency to the concrete Product class still remains. In order to properly program-to-interfaces you must remove the new instantiation, for example by using a factory or IoC.

Problem

Take this example: ``` interface IEntity { string Name { get; set; } } class Product : IEntity { public string Name { get; set; } public int Count { get; set; } // added member } class Client { void Process() { var product = new Product(); int count = product.Count; // this is valid } } ``` In the example above, what is the type of product? Is it IEntity or Product? It appears that product is of type concrete implementation (Product). If that is the case, shouldn't var be used only in special circumstances. But I see that tools like resharper recommend using var by default. Shouldn't one program to an interface?

Original source