# Monday, January 30, 2012

The var keyword was introduced with C# 3.0 and the .NET Framework 3.5 to allow the declaration of implicitly typed variables.  The driving force behind the need for implicitly typed variables was the introduction of Anonymous Types

If you are using var outside of truly anonymous types you are doing so out of laziness.  I know this is a bold statement that a lot of developers are going to disagree with, so let me explain.

First off I totally agree that using var can speed up your lines per minute coded and I do use var in this manner.  But, I always replace var with the actual type after the fact.  You can do this with out requiring a large amount of time after the fact using a tool such as ReSharper and setting up you Clean Up Code” functionality to replace all usages of var with the actual type when possible.

Now for my supporting arguments:  *All of which only pertain to using var when the type is not truly anonymous.*

  • C# is not a Dynamic Language!  This may be bad or not ideal but it is fact.  The truth is not everyone is fluent in dynamic languages and they are not expecting to read code coded in a dynamic fashion when dealing with C#.
  • Code written in a dynamic fashion, i.e. implicitly typed , is harder to read, especially when you are not use to reading it.  I know what you are saying how is this hard to read?:
    1. var x = "This is not hard to read and understand what x is!";

    And you are correct, that example is not hard to read and figure what x is.  What about this?
    1. var x = from custs in dataContext.Customers
    2.         join orders in dataContext.Orders
    3.             on custs.CustomerId equals orders.CustomerId
    4.         join lines in dataContext.OrderLines
    5.             on orders.OrderId equals lines.OrderId
    6.         join products in dataContext.Products
    7.             on lines.ProductId equals products.ProductId
    8.             where products.Name.Contains("FooBar")
    9.         where custs.LastName.Contains("Smith")
    10.         select products

    I would much rather see and read;
    1. IQueryable<Product> x = from custs in dataContext.Customers
    2.                         join orders in dataContext.Orders
    3.                             on custs.CustomerId equals orders.CustomerId
    4.                         where custs.LastName.Contains("Foo")
    5.                         join lines in dataContext.OrderLines
    6.                             on orders.OrderId equals lines.OrderId
    7.                         where lines.Quantity > 10
    8.                         join products in dataContext.Products
    9.                             on lines.ProductId equals products.ProductId
    10.                         where products.Name.Equals("FooBar")
    11.                         where custs.LastName.Contains("Smith")
    12.                         select products;

              The truth is you know what your result is, at least you better or you have no business writing the code in the first place, so just declare it as such!

              One more thing,  var has no place in demo code, tutorials, and blog posts. The purpose of demo code, tutorials, and blog posts are to provide education and in doing so the code should be clean and concise and accessible to the masses.

              OK, let me have it.

              Tuesday, January 31, 2012 9:50:02 AM (Central Standard Time, UTC-06:00)
              I am totally in agreement with you here Keith. I had a coworker argue that using 'var' everywhere is great for the case when the underlying type changes since you do not need to explicitly change it. While true, any time saved by those means is certainly not as good as knowing exactly what 'x' is, every. single. time.
              Sunday, December 09, 2012 3:17:15 PM (Central Standard Time, UTC-06:00)
              I understand very well what you what to say here. I was a lazy person all my life, but last year, after I lost my father in a car accident I've started to put my life in order, to do something with it. I sold my father's old boat and I rent a little apartment with the money. Now I have a decent job, I have few good friends and a dog. I know that this is only the beginning.
              Thursday, August 08, 2013 3:35:55 AM (Central Daylight Time, UTC-05:00)
              Thanks for sharing this useful information.
              DOT NET Training in Chennai
              Friday, December 20, 2013 9:31:41 AM (Central Standard Time, UTC-06:00)
              Relewantny obrz_k plamki. Relewantny obrz_k plamki.
              - Jeste_cie pewni, kobiety? us_ugi ksi_gowo-rachunkowe (alebiznes.net.pl) - spyta_ nieufnie
              von Egger. Opiera_ si_ o
              zapor_ podejrzane szopy na podgrodziu. - Jehtem - odpar_ sir Roger,
              troch_ nijak, wskutek trzymanej w z_bach
              dratwy. Owczy zew_ok by_ liczny. Musia_ taki istnie_, i_by umie_ci_ w samemu mn�stwo siarki, saletry,
              w_gla drzewnego jak i r�wnie_ ze trzy
              funty hufnali. Wprawdzie przejrza_e klechdy
              zaleca_y wy__czni.
              Monday, December 30, 2013 8:42:56 AM (Central Standard Time, UTC-06:00)
              In this made me exact attracted.
              Wednesday, January 04, 2017 3:59:55 AM (Central Standard Time, UTC-06:00)
              Thursday, January 12, 2017 8:57:45 AM (Central Standard Time, UTC-06:00)
              I'm new at forum, nice to meet you
              Wednesday, January 18, 2017 4:27:37 AM (Central Standard Time, UTC-06:00)
              I'm new at forum, nice to meet you
              Thursday, January 19, 2017 6:57:06 AM (Central Standard Time, UTC-06:00)
              Moja kancelaria prawna mieszcząca się w Gdańsku została stworzona z potrzeby niesienia pomocy klientom. Jako prawnik, adwokat i radca prawny posiadam gruntowną wiedzę, która umożliwia mi udzielanie niezbędnych i całościowych porad prawnych. Swoją wiedzę pozyskiwałem na kierunku prawo realizowanym na Uniwersytecie Gdańskim. Te informacje poparłem doświadczeniem w znanych kancelariach prawnych oraz miejscach porad obywatelskich w Gdańsku. Jestem prawnikiem, dla którego liczą się nie tylko sztywne paragrafy, ale także człowiek.
              Wednesday, March 22, 2017 3:02:51 AM (Central Standard Time, UTC-06:00)
              Thursday, March 23, 2017 7:08:16 AM (Central Standard Time, UTC-06:00)
              Friday, March 31, 2017 7:08:58 AM (Central Standard Time, UTC-06:00)
              (will show your gravatar icon)
              Home page

              Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

              Enter the code shown (prevents robots):

              Live Comment Preview