In the following:

public ActionResult Index()
    // Create a list of genres
    var genres = new List<string> {"Rock", "Jazz", "Country", "Pop", "Disco" };
    // Create our view model
    var viewModel = new StoreIndexViewModel {
        NumberOfGenres = genres.Count(),
        Genres = genres
    return View(viewModel);

Why was var genres and var viewModel instead of explicitly declaring their types?


6 Years
Discussion Span
Last Post by Geekitygeek

Some coders feel that 'var' reduces code noise and makes their code more readable.
In the statement List<string> genres = new List<string> the initial type declaration is redundant because the value assignment makes it clear what type the variable is. Using var gernes = new List<string> makes it shorter, more consise and more readable.
I personally dont adhere to this practice. I type all my variables in full, but maybe i'm just old fashioned. I read a debate post not so long ago that covered some of the arguments for and against, but generally i think it comes down to personal preference and what you feel makes your code more readable.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.