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.
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.
The forloop is faster than the foreach loop if the array must only be accessed once per iteration.
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 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?
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.
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.
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.
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).
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.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With