C# 3.0 : I don’t like vars

Due to my upbringing in C/C++ somehow I feel uneasy whenever I see some like

var a = 5;

I guess this is ok in scripting languages. So when I heard that C# 3.0 is going to support implicit types for local variables that made me feel uneasy. I installed the PDC C# 3.0 bits (from the link mentioned before) and tried the following

var a = 5;

var s = “hello”;



The output was as expected a was System.Int32 and s System.String. However C# 3.0 is supporting implicit types and not the variant data type that many non-typed languages support. So the following will not compile

var a = 5;

a = “hello”;

Since C# is a strongly typed language once the implicit type has been inferred during compilation it does not change.

While I do agree for things like LINQ implicit type is absolutely required but elsewhere as in the examples above vars make code more unreadable for me. Wherever I’ll see a line of code with var I scroll up-down  to figure out what type it exactly is. Specially in some situation the type of variable has a lot to do with performance so if I see some thing like

var al = Func1();
for (i = 0; i < 10000000; i++)
foreach (int el in al)
     total += el;

I’d right click on al and say goto definition and see its a var. At this point its super important for me to figure out what’s its type. If its an ArrayList and not a List<> then I have a huge perf degradation. Check out Rico Mariani’s blog to find out why.

<edited some stuff like the example above to add clarity based on the comments>
< Fixed some syntax errors >

Comments (25)

  1. Jeff Parker says:

    I have to agree with you I am not a fan of vars, I see nothing but trouble with it.

    What is wrong with the current way of just using an object. I guess I would want to know more the reason for it.

  2. Mike says:

    I don’t think var was designed to be used like in your example. The main purpose for var may be this:

    var contacts =

    from c in customers

    where c.State == "WA"

    select new { c.Name, c.Phone };

    The part new { c.Name, c.Phone } declares and instantiates an anonymous type. Additionally the query probably returns a collection of that anonymous type so there is no way do declare the variable contacts other than by using var.

  3. Des Browne says:

    C/C++ was my bread and butter when I was at uni, its explicitly typed nature was great to learn with. When I moved to c# I started having to write things like:

    System.Collections.Generics.List<int> list = new System.Collections.Generics.List<int>();

    so I very much rather writting:

    var list = new System.Collections.Generics.List<int>();

    Intellisense makes this easy and straight forward, and with out it the new fuctional and LINQ language features would be almost useless

  4. Rob Cannon says:

    vars is not a variant or something like that. It’s just a compiler shortcut where the compiler looks at what you wrote and says "You are initializing this variable with a string, therefore it must be a System.String". So the IDL emitted by the compiler is EXACTLY the same as if you had written:

    string s = "hello";

    It is misleading to say "once the implicit type has been inferred it does not change" because it could be construed that the inference happens at runtime.

    Now, you can complain that vars are not good for purposes of documentation and clear code!

  5. Cory Nelson says:

    I agree. While var is definately useful for things like LINQ, it has incredible potential for abuse.

    I fear the day years from now, when my job will be maintaining someone’s spaghetti code that uses nothing but var.

  6. After the declaration of C# 3.0 I went ahead and installed the PDC bits. After reading through the language…

  7. Pieter says:

    I’m with you on this, not mad about the var idea, at least it is not the variant data type which would really have worried me.

  8. After the declaration of C# 3.0 I went ahead and installed the PDC bits. After reading through the language…

  9. This is the my third post on the series of post I am making on C#3.0 after it got declared on PDC. See…

  10. This is the my fourth post in the series of posts I am making on C#3.0. See the previous posts here,…

  11. This is the my fifth post in the series of posts I am making on the new features in C#3.0. See the previous…

  12. Shaun says:

    What? Are C# programmers turning into VB6 programmers?

    I left VB and moved to C# after it became apparent that the VB Community wanted frilly, feel-good features like the My namespace rather than power to create amazingly powerful things in less code.

    Anonmymous types will lead to crappy code and nothing less. And we could have had default interface implementation or MI instead.


  13. jc says:

    I agree with you, the code you wrote is crappy. But it is NOT C# 3.0 legal code.

    var a;

    will be rejected by the compiler, whereas

    var a = 3;

    will be accepted.

    IOW, you always know the type of the variable by looking at its declaration.

  14. I fixed the syntax error. You need to have a assignment in the declaration of a var in C# 3.0

  15. Alex says:

    Aside: C++0x is also planning to adopt the same mechanism but using the keyword ‘auto’.

    You don’t HAVE to use it just ‘cos it’s there you know. I think it’ll save typing. But what on earth were they thinking naming it ‘var’? (Which basically means ‘variant’ to programmers via Javascript but is NOT the same in this context.)

  16. Brian Steffens says:

    Haha.. It’ll be like the "dba" (huge quotes) that stores everything as a varchar because that way it will accept any value they pass.

    I agree with Alex on the naming. ‘auto’ is a much better word for that. Or even ‘implied’. The second I looked up Linq and saw "var q = from etc" I groaned.

    I’m just happy that I don’t have to collaborate much in my job so I won’t have to deal with people using var for things that shouldn’t be anonymous.

  17. Gaurav says:

    "Due to my upbringing in C/C++ somehow I feel uneasy whenever I see some like

    var a = 5;"

    Even I had the same feeling when I see *var* in C#.

    But to be very frank it is all about the name *var* and not about what it does.

    If it was voidobj or objvoid or something C like it would have been accepted with a techie smile 🙂

  18. Matt says:

    Agreed, vars are rubbish. Okay, so it works for late-bound ‘anonymous’ types in LINQ but people are going to start using it in other scenarios making for anonymous code.

    C# is clean with a clear-cut path to achieve something so it keeps programmers egos out of the equation and makes for easy maintenance. I get the feeling that this addition is going to spoil that.

  19. lubos says:

    I don’t agree. anonymous types save you from useless class declarations which only hold data.

    for example, let’s say you have JSON web service… you can just write

    return new { Username = "hello", Password = "world" };

    no need for extra file to say what compiler can already figure out on its own.

  20. Peter says:

    var is one of the most bad things that was ever invented, becasue the lazy people that you are tryingto help are the most destructive persons in the developing team always…,

    so you are giving the killer a new weapon to use against the code…

    You really think that the person who prefer using var for ease will ever try to COMMENT his/her code….??!!!

  21. Cal says:

    This whole discussion is frustrating to read and I belive it results from uninformed developers immediate reaction to the word "var" and how it works in C# 3.0.

    There is no ambiguity about the type of any variable declared with the "var" keyword. It MUST be initialized in the same statement. The value that the variable is being initialized to determines the type of the variable.

    Furthermore, please understand that C# is still strongly typed. This is not a late-bound typoe or a variant, everything is known by the compiler at compile-time. This is not the introduction of the nasty variant datatype into C#!

    I have still not decided if the var keyword should ever be used outside of the declaration for variables of anonymous types… I don’t see why it shouldn’t be used for most variables defined inside methods.

  22. Jax says:

    to quote the MSDN documentation:

    "Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types."


  23. Mihai Danila says:

    Years ago, someone was telling me how he still liked the old C/C++ way of structuring their methods: the variable declarations at the top, and the code using them down below. To this, add the use of cryptic one-letter variable names and you’ve got your recipe for disaster.

    I’m not bringing this up just to point out how var forces you out of this pattern (though this is in itself a good point,) but rather to point out that writing readable code involves the use of policies by developer that are currently beyond what can be enforced by compilers.

    I hate to have to say it, but I’m never going to write with variable names like the one in the example. I always use descriptive variable names and descriptive method names. This is a matter of policy, policy that should be in place already at your workplace. Methods that grow large should be split into smaller methods, again, to enhance readability: yet another policy that you should have in mind when coding.

    The point you are trying to make here becomes more and more mute as we investigate each claim. Until the compiler learns to enforce the use of good variable names, I agree that there is much potential for writing cryptic code through the use of var, but that’s as far as you should go with the claim. The decision of whether to use var should be made on a case by case basis. My feeling is that, if you’re good enough of a coder to write elegant code, var won’t stop you from getting there. On the contrary, it may well help alot.

  24. Mihai I do not aggree with you. You assumption is that all code is written by you or your immediate team. Unfortunately that is not the case in the real world. Code bases grow and move ownership. Soon you will land up having to own/debug/re-use/use other teams code who are not deligent in imposing those policies.

    With what you said the responsibility of language designers go away. Then why don’t we have typedefs in C#?

  25. Waleed Eissa says:

    Totally agree with you, it makes the code far less readable (and actually makes look like JavaScript more than C#), it should only be used with anonymous types but no where else.

    It’s already being abused, go to stackoverflow.com and view some C# related questions to see what I mean.