Var Wars – Abuse of the CSharp var

CSharp 3.0 introduced the var keyword for declaring variables without having to explicitly specify the type. This was done for using anonymous types returned from LINQ queries. Now, I am seeing many developers use it all over their code and think its a good thing.

Wasn’t C# developed to enforce strongly typed programming?

Var just makes it harder to infer the type when reading and maintaining code.

Take for example this LINQ Query:

var user = from u in Users where user.Status == UserStatus.Active
select u;
It is hard enough to infer the type from the above query, let alone infer the type from a statement like this:
var index = 1; (Is index an int, long?)
Just because a compiler can infer the type does not mean a human should have to be forced to do it when reading it. If we wrote code just to make compilers happy we should be writing Assembly. Why then use an Object Oriented Language? But since code is written for developers to be able to read and maintain it and not just for compilers to compile it, it should be as descriptive and readable as possible.

Some say it cuts down on repetitive code when used in a statement like this:

var user = new User();

For those who are so obsessed with cutting down characters and lines of code, here is a small piece of advice that will better save you coding time and characters: focus on better design and architecture. If you are so concerned about repetitive code – your energy will be well rewarded. Well designed code is always shorter, cleaner, easier to read and also works better!

Cutting down on type names is like removing the wrapper from a drink. You have to infer what is in it by drinking it. For those who don’t care about their calories go ahead, but for the rest us, please put the wrappers!

The only time you really have to use var is for anonymous types. There is no other way to get an anonymous type. For everything else a type name can and should be used for variable declaration.

So for those var abusers, spare the rest of us some inferring and just declare your variables with a type name.


  1. Bah. Well-named variables negate the name for an explicit type in the declaration. This is especially true for assignments with constructor calls on the right hand side. For numeric types, use the proper suffix on literals (1f, 1l, 5.00m, etc) to help readers infer the type. Using 'var' greatly improves code readability--blocks of variable declarations are more easily identified because the variable names are all left-aligned. If inferring the types is really that burdensome, try using 'var' combined with an explicit cast (var user = (User)Some.Property;). Or just hover the mouse cursor over any variable usage to see its type.

  2. "(var user = (User)Some.Property;)"

    _That_ is retarded.

    "just hover the mouse cursor over any variable usage to see its type"

    So now I have to pick up the mouse to figure stuff out?

    No, var abuse is rife and damn stupid. It doesn't aid readability one bit.

  3. Love the post. I'm fighting a proliferation of its use at the office right now and stumbled on your blog looking for common sense reasoning to what I intuitively felt was a bad idea.

    Thank you!

  4. thank you for sharing, this is a compelling website content interesting

  5. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  6. I like the post format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for gauging the informative posts.
    cara menggugurkan kandungan
    cara untuk menggugurkan bayi

  7. We find lots of learning after reading this very useful article . cara menggugurkan hamil

  8. I thank you for the information and articles you provided

  9. Welcome to our XXX VIDEO agency where we unveil our Secret Babes. We are an Hentai tube x agency who cares very much about our reputation and do everything to maintain our standards to the very highest level. After many years in the business we like to think we know what we are doing and have got everything right and co-ordinated. We are now much more of a regional Shush escorts agency covering the North West of England and have amassed a collection of beautiful and dedicated professional companions who love what they do. We have learnt over the years that the best Porn video are the young ladies that enjoy what they do and treat it more as a vocation or hobby.

    As we are such a high profile and reputable sex toys shiop in Manchester we receive an inordinate amount of applications from prospective escorts from throughout the region. However, we can afford to be very selective and go for those ladies with outgoing personalities and social skills as well as good looks. Beauty is however in the eye of the beholder, so consequently we aim for a good balance of physical attributes. Above all we are looking for ladies where XXX VIDEO a true extension of their normal lifestyle and are comfortable chatting to and mixing with a variety of other people.

  10. cool, please guidance so that I can create a blog like yours

  11. We sell original Herbal Products composed of Natural Herbs Clinic obtained from the world’s top botanic gardens under the cautious administration of experts who are working day and night to make the herbal products even better and to increase the range so that people suffering from certain ailments which do not have any identified care as yet can benefit from our formulas.

  12. Lucky7 Casino Site Review: A New Player Casino Bonus
    Lucky7 worrione casino is an online casino 1xbet that is fully equipped choegocasino with all the exciting games in a fun and secure environment. It offers you all the best

  13. Tekhnologi News is a guide and recommendation blog on the most popular switches , sound card , wifi adapters , best convertible laptop , webcams , hdmi cables technology this year.