Possible Duplicate:
C# ‘var’ vs specific type performance
When I write below code:
List<string> list = new List<string>();
resharper wants to convert it to
var list = new List<string>();
Why? What is the difference?
Is it more understandable? I think not.
Using var has more than the other cost on RAM. Isn’t it?
They are the same.
varis implicitly typed.If you hover over the keyword
varin Visual Studio, it will show you the type of your object – in this caseList<string>. The use ofvaris only to clean up code – you already know you’re creating an object of typeList<string>, so some people think it is redundant to type:There is no performance difference, as the compiler already knows what type the object is. Using var personal preference mostly – you can use it if you want to and there is no performance hit.
Eric Lippert has a great blog post about
varhere.I think you’re confusing
varanddynamic, which are two totally different things. Thedynamictype is a type that allows you to assign values of different types to it at runtime.