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

Sep 11, 2012

Tuple in C#

C# 4.0 included a new feature called Tuple.

A tuple is a data structure that has a specific number and sequence of elements. Tuple can be used in several ways.

In this post, I am sharing an example, how tuple can be used to return multiple values from a method.

In the example, we are passing two int values to the method and method is performing four mathematical operations (add, subtract, multiplication, division) and returning a tuple with 3 int and 1 double values for these operations.
using System;

namespace ConsoleApplication1
{

    class clsMain
    {
        static void Main(string[] args)
        {
            clsTuple objTuple = new clsTuple();
            var tuple = objTuple.Operations(10, 3);
            Console.WriteLine("Sum:{0}, Subtract:{1}, Multiply :{2}, Division:{3}", tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4);
            System.Threading.Thread.Sleep(2000);
        }
    }

    class clsTuple
    {

        public Tuple<int, int, int, double> Operations(int i, int j)
        {
            return Tuple.Create(i + j, i - j, i * j, Convert.ToDouble(i) / j);
        }      
    }
}
OUTPUT