-1
        var Id = from value in Enumerable.Range(0, 100)
                              select value;

        IEnumerable<int> ids = from value in Enumerable.Range(0, 100)
                              select value;



        foreach (var v in Id)
        {
            Console.WriteLine(v.ToString());
        }

        foreach (var x in ids)
        {
            Console.WriteLine(x.ToString());
        }

Both will do the same job, but what is the difference?

  • 5
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Dec 10 '13 at 12:00
  • 3
    See Use of var keyword in C# for some information. – CodeCaster Dec 10 '13 at 12:13
  • 2
    They are exactly the same, var is strongly type but leaves the typing of the variable to the compiler. In this case the compiler would choose the type IEnumerable<int> . You could have fun creating some tests and seeing which one is more efficient in memory usage or access speed or whatever metric you chose, but they are the same. – AlexC Dec 10 '13 at 13:41
  • 4
    @MohammedBilal that's because there is no difference between them, var is just syntactic sugar so that you don't have to explicitly specify the type of the variable. the type will be inferred from the expression that follows the = operator. – devnull Dec 10 '13 at 13:51
  • 1
    This sort of question is exactly the reason why you should only use var where the type is made explicit by the right side of the assignment. – Mr Cochese Dec 10 '13 at 13:59
6

There is no difference between the two, except for readability. The second example (ids) is more explicit than the first and perhaps more readable. But, readability is a matter of taste/opinion with many different viewpoints. Here are some guidelines...

 public void SampleCode()
        {
            //Use of var is encouraged when  declaration needlessly clutters code
            Widget widget1a = new Widget();  //No
            var widget1b = new Widget();  //Yes, I know I am getting a widget.

            //Use of var is encouraged when method name defines return type 
            //or return is type is known without further need of code inspection
            Widget widget2a = GetWidget(); //OK (Verbose)
            var widget2b = GetWidget(); //Yes, But...
            //Danger, Will Robinson!  if GetWidget returns a foo...Code review time!

            //Use of var is discouraged when method offers new clue to type 
            //without further code inspection
            Widget widget3a = Process(); //Yes, intent is clear without any further inspection.
            var widget3b = Process(); //No, What does Process do?
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.