Var in C#


I've recently upgraded to R# 4.0. I tried it in the past and the early EAP's were a bit flaky, but lately they are getting better and better. Since this version has support for C# 3.0, every time you declare a variable and create an instance of the same class, you get presented with a hint to change the declaration to use var. That is, when creating an instance of the class Parent as shown

R# suggests you convert it to

Initially I was reluctant to do something like that, but the more I think about it, the less I see anything wrong with it. I think it's perfectly fine to not have to declare of what type the variable is if you're creating an instance of the same type.

Comments: 

Magnus Akselvoll 2008-04-28 05:53:00
I like this syntax as well, since I find it just as readable and more flexible (e.g. if you want to change MyType to MyCachedType you only change it once).

However, in sentences where the type is not explicitly written on the right hand side, such as "DateTime dt = XmlConvert.ToDateTime(s);" i prefer not using var.


Add your comment: 

Name:  
E-Mail:  
URL:  
Comment:  
Please add 8 and 4 and type the answer here: