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;
Using
varinstead ofIEnumerable<T>can make a performance difference in the case when the type of the expression on the right side is notIEnumerable<T>.In your code above (assuming
dataBase.CaseTablesisIQueryable<CaseTable>backed by LINQ to SQL or some similar provider), if you wrotetest.First(), you could retrieve lots of rows from the database to get only the first one.If you used
varorIQueryable<CaseTable>, you would retrieve only the first row.This can make difference even without
IQueryable<T>. For example,List<T>.GetEnumerator()returns a customstruct. Iterating using that is going to be faster than iterating throughIEnumerator<T>. Although the difference is going to be negligible most of the time.What this all means: yes, using
varcan make a difference: it can make your code faster, in some cases much faster.