It is getting ridiculous:

var input = "This is a  test";
  • 19
    I like var. If var could be boiled down into a syrup, i would pour it over my waffles and eat them in front of you, the resulting enjoyment plainly visible on my face. – Shog9 Sep 2 '09 at 4:18
  • 5
    Any particular reason you're against var? Or are you just an anti-varist? – Jared Harley Sep 2 '09 at 4:23
  • 2
    It is not meant for every instance declaration, just ones where you don't know or a type doesn't exist yet. It litters code and is confusing to other developers. – Yuriy Faktorovich Sep 2 '09 at 4:52
  • 1
    How is var input = "This is a test"; any more confusing than string input = "This is a test"; ? – Matt Hamilton Sep 2 '09 at 6:00
  • @Matt, try that on c# 2.0. No one is attacking var, it a riot. But of course it can get confusing, and can be abused. – Kobi Sep 2 '09 at 6:25
  • @Yuriy Faktorovich - perhaps you should edit the question, change it to a discussion about var use in SO. I agree there's an issue here, but you didn't put up any valid points. – Kobi Sep 2 '09 at 7:02
  • @YuriyFaktorovich Reading your comment, it seems you're confusing var with dynamic. You say you should use var when you don't know the type yet, that is not even possible. var input = null; does not compile: "Cannot assign 'null' to an implicitly-typed local variable". – Rudey Jan 3 '17 at 8:44
  • @RuudLenders I'm pretty sure at the time of my posting the question dynamic wasn't a thing. – Yuriy Faktorovich Jan 3 '17 at 14:09

There's no doubt that var can be overused. However, it can also aid readability by emphasizing how a variable is used rather than what its exact type is, and by reducing duplication.

Eric Lippert wrote a great comment about this topic when he was tech reviewing C# in Depth.

var has another very handy property in places where space is constrained - it's almost always shorter than the type name. This can be important in books and also on Stack Overflow where line lengths are fairly short.

More importantly, however, I don't think it's up to any one person to dictate the coding style used for answers. Go ahead and avoid using var in your own answers. Criticise answers where the use of var really, really leads to confusion... but accept that it's a personal preference.

| improve this answer | |
  • Excellent quote from Eric Lippert! – Brian Rasmussen Sep 2 '09 at 7:06
var answer = "no";
| improve this answer | |
  • 3
    how about var answer = false; ? – Jarrod Dixon Sep 2 '09 at 4:51
  • 1
    Isaac Waller ♦ – deleted Sep 2 '09 at 5:17
  • 3
    How about var answer = processDiscussion()? I hope that's a IEnumerable<Conclusion>, but it could be "no", 1... – Kobi Sep 2 '09 at 7:07
  • var answer = "false"; would be better. Confirms the OPs point ;-) – Cory Charlton Feb 18 '10 at 20:33

Actually, I've noticed that too. Here's an extreme example:

Someone edited another user's answer from:

Int32 count = Enumerable.Intersect(listA, listB).Count(word => word.Length > 3);


var count = Enumerable.Intersect(listA, listB).Count(word => word.Length > 3);

I don't see any reason to do that - I wouldn't edit an answer to fit my coding standards.
As for weather var is good or not, and best practice: that belongs on SO, but it has its downsides. Personally, I try not expose too much of it to beginners (or it gets too confusing / magical), especially not instead of ints (as above) .

| improve this answer | |
  • 6
    int32? maybe it should have been an int64, or perhaps an int128 with blast processing! Kidding aside, I agree that editing in var into someone else's code isn't very cool. – Jeff Atwood Sep 2 '09 at 8:30

Not the answer you're looking for? Browse other questions tagged .