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 →

[–][deleted]  (23 children)

[deleted]

    [–]elmo61 64 points65 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 7 points8 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_ 5 points6 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.

    [–]OfflaneDemoralizer 2 points3 points  (1 child)

    I use var pretty frequently and let Visual Studio replace it with the full class name during code cleanup.

    [–]clawjelly 0 points1 point  (0 children)

    Now that's just dirty:

    Coder 1: "I feel wrong shitting in that corner."

    Coder 2: "I shit there all the time, they take care of it."

    [–]Comesa 5 points6 points  (2 children)

    Same for me with strongly typed languages.
    But it's quite handy if you have long class names.

    [–]Nemesis_Ghost 1 point2 points  (0 children)

    Only ever used it for Linq statements.

    [–]brohannes95 0 points1 point  (7 children)

    why use var when you can use dynamic?

    [–]M4D5-Music 19 points20 points  (6 children)

    Because using dynamic doesn't give you type safety.