Var versus IEnumerable in LINQ

We have seen normally
clip_image002
Or
clip_image004
Now question is where to use var or IEnumerable? Let us first have a look on both key words
Var derives type from the right hand side. And its scope is in the method. And it is strongly typed. IEnumerable is interface which allows forward movement in the collection.
Now in LINQ where to use what ?
If we really do not have idea that what would be the type of query then we will be using var. and if we are sure about the type of output then we would be using IEnumerable .
Using IEnumerable <T>
In the below example, we are querying in collection of string so we know the output would be collection of string, so we are using here IEnumerable with string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lstStrings = new List<string> {"Dhananjay Kumar",
                                                         "Mahesh Chand",
                                                         "John papa",
                                                         "Mike Gold",
                                                         "Shiv Prasad Koirala",
                                                          "Victor ",
                                                           "Pinal Dave" };
            IEnumerable<string> result = from r in lstStrings select r;
            foreach (var r in result)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey(true);
        }
    }
}

Output
clip_image006
Using var
Now if we don’t know exactly what would be the type of result in that case we will be using var. There might be some scenario where you want to return anonymous type from the query
clip_image008
If you see the above query we do not know the type of result.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DataClasses1DataContext context = new DataClasses1DataContext();
            var result = from r in context.Persons select new { NameOfperson = r.FirstName + r.LastName };
            foreach (var r in result)
            {
                Console.WriteLine(r.NameOfperson);
            }
            Console.ReadKey(true);
        }
    }
}

Output
clip_image010
About these ads

4 thoughts on “Var versus IEnumerable in LINQ

  1. Hi Dhananjay

    Nice post. Keep it rolling. Liked your concept of telling the difference.

    Lately with the introduction of the keyword var – its a good practice to use the var on the left hand side. Advantage/Benefit is that the code becomes more readable. Meaning consider the following:
    Assume that i have a class lets say “AmVeryLongNameClass”.
    so if you are trying to new this up, it would look like
    AmVeryLongNameClass variable = new AmVeryLongNameClass()
    instead if we use var
    var someObject = new AmVeryLongNameClass()

    Many insidres in MS propogate this. I too follow this extensively. Its just a programming design from my perspective.

    Having said that – the scenario you have talked about does capture the gist of when to use what.

    Keep the posts rolling.

    regards
    Lohit

  2. Cool!! Nice explanation. But I am not sure of one thing though, var resolves it’s type at runtime, so I guess it might depending on Reflection to get the type resolved. So from the performance perspective I am not sure if it would be efficient. However, default rules of Re-Sharper’s newer version suggests using var instead of strongly typed variables. And moreover, you are right, when we don’t know the type of the result then it surely helps to use var.
    Nice post though.
    Keep it coming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s