Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

faster way between to ways of iterating through all the elements of a collection in C# [duplicate]

Tags:

c#

foreach

The language I use is C#.

Let we have a List of objects of type T,

List<T> collection = new List<T>{.....};

Say that we want to go over each item of collection. That can be done in many ways. Among of them, are the following two:

foreach(var item in collection)
{
   // code goes here
}

and

foreach(T item in collection)
{
    // code goes here
}

Does the second way be better than the first or not and why?

Thanks in advance for your answers.

like image 994
Christos Avatar asked Dec 09 '13 14:12

Christos


People also ask

Which method is used to iterate through the items in the collection?

An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the yield return statement to return each element one at a time.

Which is faster foreach or for loop C#?

The forloop is faster than the foreach loop if the array must only be accessed once per iteration.

Are for each loops faster?

As it turned out, FOREACH is faster on arrays than FOR with length chasing. On list structures, FOREACH is slower than FOR. The code looks better when using FOREACH, and modern processors allow using it. However, if you need to highly optimize your codebase, it is better to use FOR.

What are the two ways to iterate through a collection?

What are the two ways to iterate the elements of a collection? There are three common ways to iterate through a Collection in Java using either while (), for () or for-each (). Which method is used to iterate over each element?

How do you iterate through a collection of objects in Java?

There are three common ways to iterate through a Collection in Java using either while (), for () or for-each (). Which method is used to iterate over each element? Method 2: Using iterator An iterator is an object in Java that allows iterating over elements of a collection.

How do you iterate through a list in C?

Iterators (C#) An iterator can be used to step through collections such as lists and arrays. An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the yield return statement to return each element one at a time.

How do you iterate over a collection in Python?

An iterator method or get accessor performs a custom iteration over a collection. An iterator method uses the yield return statement to return each element one at a time. When a yield return statement is reached, the current location in code is remembered. Execution is restarted from that location the next time the iterator function is called.


2 Answers

They're both exactly the same. var is syntactic sugar for convenience. It makes no difference to the speed with which a List is traversed.

The rule of thumb I follow with var is to only use it if the type of the object is present on the right-hand side of an assignment, so in this case I'd prefer to explicitly specify the type in the foreach to make it clearer for other engineers, but it's down to personal choice. If you hover over a var in Visual Studio, it will display the type (assuming it can infer what is should be).

like image 86
Chris Mantle Avatar answered Sep 22 '22 20:09

Chris Mantle


Quoting MSDN:

An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

So

var i = 10; // implicitly typed
int i = 10; //explicitly typed

Are exactly the same.

Now, for 'better' - It'll heavily depend on what's your parameter to judge that. If it's speed, then a for loop may be better than a foreach, and T[] better than List<T>, according to Patrick Smacchia. Main points:

  • for loops on List are a bit more than 2 times cheaper than foreach loops on List.
  • Looping on array is around 2 times cheaper than looping on List.
  • As a consequence, looping on array using for is 5 times cheaper than looping on List using foreach (which I believe, is what we all do).

Quote source: In .NET, which loop runs faster, 'for' or 'foreach'?

Reference: http://msdn.microsoft.com/en-us/library/bb383973.aspx

like image 41
OnoSendai Avatar answered Sep 24 '22 20:09

OnoSendai