Jaa


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";Console.WriteLine(a.GetType());Console.WriteLine(s.GetType());

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

  • Anonymous
    September 15, 2005
    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.

  • Anonymous
    September 15, 2005
    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.

  • Anonymous
    September 15, 2005
    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

  • Anonymous
    September 15, 2005
    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!

  • Anonymous
    September 15, 2005
    The comment has been removed

  • Anonymous
    September 15, 2005
    After the declaration of C# 3.0 I went ahead and installed the PDC bits. After reading through the language...

  • Anonymous
    September 15, 2005
    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.

  • Anonymous
    September 15, 2005
    After the declaration of C# 3.0 I went ahead and installed the PDC bits. After reading through the language...

  • Anonymous
    September 16, 2005
    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...

  • Anonymous
    September 17, 2005
    This is the my fourth post in the series of posts I am making on C#3.0. See the previous posts here,...

  • Anonymous
    September 18, 2005
    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...

  • Anonymous
    October 06, 2005
    The comment has been removed

  • Anonymous
    October 14, 2005
    The comment has been removed

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

  • Anonymous
    August 01, 2007
    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.)

  • Anonymous
    August 31, 2007
    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.

  • Anonymous
    October 25, 2007
    "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 :)

  • Anonymous
    November 20, 2007
    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.

  • Anonymous
    January 05, 2008
    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.

  • Anonymous
    January 16, 2008
    The comment has been removed

  • Anonymous
    January 23, 2008
    The comment has been removed

  • Anonymous
    May 23, 2008
    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." http://msdn.microsoft.com/en-us/library/bb383973.aspx

  • Anonymous
    June 30, 2008
    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.

  • Anonymous
    June 30, 2008
    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#?

  • Anonymous
    April 03, 2009
    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.