0

Possible Duplicate:
C# 'var' vs specific type performance

In term of performance does it make any difference to use var or IEnumerable in linq? what about IEnumerable or IQueryable? how can we know which one is IEnumerable and which one is IQueryable? for ex: in code below

 IEnumerable<CaseTable> test = from x in dataBase.CaseTables
                               where x.case_id.Equals("12")
                               select x;
Community
  • 1
  • 1
Daniel
  • 3,322
  • 5
  • 30
  • 40
  • 1
    No. And expectedly it has been discussed here a lot of times: http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c. In your case you have `IEnumerable` as long as [Select](http://msdn.microsoft.com/en-us/library/bb548891.aspx) returns `IEnumerable` – zerkms Aug 14 '11 at 14:13
  • @zerkms That question doesn't ask anything about performance. – Grant Thomas Aug 14 '11 at 14:15
  • 1
    @Mr. Disappointment: I was too lazy to find one, but here it is: http://stackoverflow.com/questions/356846/c-var-vs-specific-type-performance ;-) – zerkms Aug 14 '11 at 14:16
  • @zerkms it really can - see the example I added – Marc Gravell Aug 14 '11 at 14:23
  • @Marc Gravell: yep, already did and +1'd you. Never experienced it though irl (and obviously that is why I mentioned "No") and even more - it looks like a big surprise for me – zerkms Aug 14 '11 at 14:24

4 Answers4

7

It will make a difference if the return type isn't the same, and this then changes composition. For example:

IEnumerable<Users> users = db.Users;
var count = users.Count();

versus:

var users = db.Users;
var count = users.Count();

If this is actually a DB provider, the second should issue a "select COUNT(1) from Users" (1 row, 1 column) - however, the first will issue a "select * from Users" - n columns times m rows. It will drag the data over the network, materialise it into objects, and then (LINQ-to-Objects) count the objects.

If it was already IEnumerable<T> then it will make no difference.

Re knowing what it is - hover ;) or better: use a profiler (such as mvc-mini-profiler) so that you can keep a close eye on the queries it is running at all times.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
4

var is only syntax for the programmer - it doesn't change the semantics at all. If you declared as var, the type of test is still IEnumerable<CaseTable> in your example.

As for whether it's IEnumerable or IQueryable - that depends on the type of dataBase.CaseTables

Mark H
  • 13,797
  • 4
  • 31
  • 45
  • Actually, in the example I imagine var would use IQueryable-of-T – Marc Gravell Aug 14 '11 at 14:21
  • @Marc could you tell me how you come out with this idea? I can't understand the the difference between IQueryable and IEnumerable. even in msdn.microsoft.com most of the examples use var. – Daniel Aug 14 '11 at 14:29
  • @Daniel IEnumerable uses LINQ-to-Objects. In order to use the composing interface, the stronger IQueryable interface must be used. This has different implementations of Count, Where etc that create a composed query, rather than daisy-chained object methods – Marc Gravell Aug 14 '11 at 14:43
  • @Daniel basically, it comes down to whether Enumerable.{method} or Queryable.{method} is used – Marc Gravell Aug 14 '11 at 14:44
3

Using var instead of IEnumerable<T> can make a performance difference in the case when the type of the expression on the right side is not IEnumerable<T>.

In your code above (assuming dataBase.CaseTables is IQueryable<CaseTable> backed by LINQ to SQL or some similar provider), if you wrote test.First(), you could retrieve lots of rows from the database to get only the first one.

If you used var or IQueryable<CaseTable>, you would retrieve only the first row.

This can make difference even without IQueryable<T>. For example, List<T>.GetEnumerator() returns a custom struct. Iterating using that is going to be faster than iterating through IEnumerator<T>. Although the difference is going to be negligible most of the time.

What this all means: yes, using var can make a difference: it can make your code faster, in some cases much faster.

svick
  • 236,525
  • 50
  • 385
  • 514
  • I think I'm still confused about the IQueryable and IEnumerable. how can I differentiate between them? – Daniel Aug 14 '11 at 14:24
  • I'm not sure I understand, but I think the differences between `IEnumerable` and `IQueryable` are beyond the scope of your original question and that other people already explained them much better than I would. Just use google to search for them. – svick Aug 14 '11 at 14:28
2

The keyword "var" is just syntactic sugar in C#. The variable declared by that is still statically typed, and the compiler will infer its type.

Type obj = new Type() and var obj = new Type() are the same.

"var" is usefull to make your code less bloated when the type of the variable is evident from its declaration. It's also very nice when you want to discover the not-so-obvious type of a variable (via IntelliSense) - "LINQ" rings some bells.

Of course, restrictions apply to when you can use 'var'. Have a look here.

Vladimir
  • 3,599
  • 18
  • 18