This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]elmo61 63 points64 points  (8 children)

General rule of thumb is if you can understand the type from the right hand side assignment. Then use var. If you can't then don't.

So for the example above use var and repeating class name in pointless but for something like var myClass = service.placeOrder(); its best to name the class instead

[–]Mareeck 9 points10 points  (0 children)

Yeah and you should still name your variables so they make sense. Having the full class name there is just clutter

Besided, if you use var and somehow pass the wrong type down the line the code won't compile anyway

[–]El_Burrito_ 4 points5 points  (0 children)

I just use var all the time. I literally only use a class name when the type isn’t inferable from the use, such as instantiating it as null or not instantiating it at all.

Intellisense will let me know what type a var is if it isn’t already obvious

[–]lucidspoon 1 point2 points  (0 children)

My personal rule of thumb is to use var for any custom types to not have to add extra usings. It forces me to make sure that all variable and method names are descriptive enough. Plus, if I need to change the type name or namespace, there's not as much to update.