Click here to Skip to main content
Click here to Skip to main content

Tagged as

Best Practices on Using Implicitly Typed (i.e. var ) and Explicitly Typed Variables

, 4 Nov 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Best practise using var


Leveraging the type inference is wonderful when writing code. With the arrival of ‘var’ in C# 3.0, it now allows a developer to write more relaxed code.

int i = 2 can now be written as var i = 2, without having to worry about the overhead for boxing/un-boxing.

The compiler infers the type indicated by ‘var’ from the expression used to initialize the variable, and the IL code contains the inferred type. However, it is not always the best thing to use var. While using ‘var’ can help developers to avoid all the type declarations for local variables, it has an impact on code readability. Consider invoking an overloaded method, which differs in the parameter type. If the method is invoked with parameters declared as ‘var’, it makes the code complex to understand.

Consider the following snippet:

private int GetUserInput()
    Random r = new Random(1);
    return r.Next();

private int Add(int numberOne, int numberTwo)
    return numberOne + numberTwo;
private float Add(float numberOne, float numberTwo)
    return Convert.ToInt32(numberOne + numberTwo);

private void InvokeAdd()
    var numberOne = GetUserInput();
    var numberTwo = GetUserInput();
    var addedNumber = Add(numberOne, numberTwo); //version of ‘Add’ method is not evident

Which version of ‘Add’ method is invoked is not evident at the first glance, in the above snippet.
Although the best practices for using ‘var’ cannot be made generic but a little guidance can help in making a conscious choice. Below are a few thoughts on when to use or not to use ‘var’.

  1. Do use ‘var’ to:
    1. Refer anonymous types, e.g.
      var anonymousType = new { Name = "Dilbert" };
    2. Refer query expressions, e.g.
      var queryExpression = from c in customers where c.Name == "Dilbert" select c;
    3. Refer complex generic types, e.g.
      var searchList = new Dictionary<string>();
  2. Do not use ‘var’ to:
    1. Refer known types, e.g.

      var customer = new Customer(); //Do not use 
      var numberList = new int[] { 1, 2, 3, 4, 5 }; //Do not use.
    2. Refer constants, e.g.

      var i = 5; //Do not use
    3. Refer simple expression assignments, e.g.

      var count = customers.Count();//Do not use
      var customerName = customer.Name;//Do not use
    4. Refer variables where types cannot be inferred or where inferred type is not what is intended, e.g.

      IList<customer> customers = new List<customer />(); //Do not use

      Consider code readability and maintainability while making a choice between ‘var’ and explicit type.


  • 4th November, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


No Biography provided

Comments and Discussions

GeneralMy vote of 1 PinmemberChantiPDM14-Jan-11 6:05 
GeneralMy vote of 1 PinmemberRichard Deeming12-Nov-09 10:00 
General[My vote of 1] Мethodically, socially and morally incorrect PinmemberSAKryukov9-Nov-09 7:51 
GeneralSuch an overreaction... PinmemberCurtainDog9-Nov-09 20:45 
GeneralRe: Such an overreaction... PinmemberSAKryukov10-Nov-09 6:43 
GeneralMy vote of 1 Pinmemberbrian_agnes9-Nov-09 7:04 
GeneralShould be based on facts and reasoning PinmemberSAKryukov9-Nov-09 7:36 
GeneralMy vote of 2 PinmemberBen Robbins8-Nov-09 22:48 
GeneralRe: My vote of 2 [modified] PinmemberATANU.PODDER9-Nov-09 0:15 
GeneralMy vote of 2 Pinmemberdequadin7-Nov-09 7:52 
GeneralRe: My vote of 2 PinmemberATANU.PODDER9-Nov-09 0:27 
GeneralI understand the boxing reference PinmemberMember 36807856-Nov-09 6:09 
GeneralRe: I understand the boxing reference Pinmemberdberindei8-Nov-09 23:24 
GeneralRe: I understand the boxing reference PinmemberMember 36807859-Nov-09 7:36 
GeneralRe: I understand the boxing reference Pinmemberdberindei9-Nov-09 22:23 
GeneralRe: I understand the boxing reference [modified] PinmemberMember 368078511-Nov-09 8:45 
GeneralRe: I understand the boxing reference Pinmemberdberindei12-Nov-09 0:04 
GeneralRe: I understand the boxing reference PinmemberIndrora23-Nov-09 17:10 
GeneralRe: I understand the boxing reference Pinmemberdberindei23-Nov-09 22:42 
GeneralRe: I understand the boxing reference PinmemberIndrora4-Dec-09 17:12 
GeneralRe: I understand the boxing reference Pinmemberdberindei9-Dec-09 3:17 
GeneralWhy comparing "object" with "var"? PinmemberSAKryukov9-Nov-09 7:33 
GeneralRe: Why comparing "object" with "var"? PinmemberMember 368078511-Nov-09 10:39 
GeneralRe: Why comparing "object" with "var"? PinmemberSAKryukov11-Nov-09 13:14 
GeneralMy vote of 2 PinmemberUtwig6-Nov-09 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 4 Nov 2009
Article Copyright 2009 by ATANU.PODDER
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid