Sep 26, 2012

var in c#

In this post, i am trying to unleash the exact use of the var keyword.

In .Net, when you simply declare a variable with datatype int, it is explicit declaration
int i = 0; //explicit
but when declared with var, compiler looks for the data assigned during declaration and accordingly  assign appropriate datatype to it during compilation process.
var v = 0; //implicit
For example, In above declaration, we are assigning numeric value 0 to var, so var would be replaced to int during the generation of IL Code. var define datatype statically not on run-time. In other words, they are not like objects which can once point to int, then string on run-time. variable declared with var is initialized within the declaration statement and can't be initialized with null.

Why use var?
The most important aspect of var in existence is LINQ with Anonymous Type. Using var makes your code very simple and short.

Let's first take an example code of LINQ with Anonymous Type without using var
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        class Details
        {
            public int Length;
            public string Value;
        }

        static void Main(string[] args)
        {
            string[] Names = { "Sandeep", "Abhay", "Ritesh" };
            IEnumerable<Details> details = from x in Names select new Details { Length = x.Length, Value = x };
            foreach (Details d in details)
            {
                Console.WriteLine(string.Format("Name : {0}, Length : {1}", d.Value, d.Length));
            }
            Console.Read();
        }
    }
}
In the above code, we have created a strongly typed object which we can get from a LINQ query. But, the problem is that we have written a lot of code, created a class, then put the class in IEnumerable and then we am getting property. Now using the var the same can be achieved simply with less code. The biggest advantage of using var with LINQ and Anonymous time is that we do not need to create class Details anymore.

Example of LINQ with Anonymous Type using var
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Names = { "Sandeep", "Abhay", "Ritesh" };
            var v = from x in Names select new { Length = x.Length, Value = x };
            foreach (var d in v)
            {
                Console.WriteLine(string.Format("Name : {0}, Length : {1}", d.Value, d.Length));
            }
            Console.Read();
        }
    }
}
OUTPUT

    Choose :
  • OR
  • To comment
3 comments:
Write Comments

Probabilmente come studente universitario ti troverai a scrivere una o più tesi per il tuo analogo esame di fine carriera. Ma di cosa si tratta in particolare? Scopriamo quali sono i pro dei tool per riscrivere testo che troverai online. Per strumento di parafrasi si intende un tool online che si occupa di parafrasare in maniera immediata frasi oppure interi paragrafi. Questa attività si svolgerà interamente su internet e non lascerà alcuna traccia di plagio