How to decide between using for or foreach?

Asked

Viewed 3,081 times

12

When should I choose between using the for or foreach?

They both do pretty much the same thing, and I always wonder if I’m using the "correct"...

My doubt increases when people say to use the for for "be more performatic" and others say to use the foreach the code becomes more readable. However the @Maniero shows that the opposite can happen in this answer, and that the foreach may be faster than the for.

  • So, how can you know when to use one or the other on issues such as performance, flexibility, easy maintenance and integration for example?
  • What parameters should I analyze or take into account to decide which can be most advantageous?
  • I think this difference in performance is so minimal, and it’s never the bottleneck in software, we usually do other things so grotesque that this is tiny.

  • @Dorathoto The doubt is not just about performance, I edited the question, to see if it became a little easier to enter what I want :)

  • If you could put a context, a code, a particularity, because, I think it’s more speculation than a fact, see, the question is good, but, will generate many opinions because it lacks the context of application. Use for or foreach is more layout than right or wrong.

  • I don’t know if it helps, but as a general rule I follow the following, when I don’t know how many elements I will iterate in the loop I use foreach, when I know how to use For, on the other hand the foreach uses more resources in iterations

5 answers

18


I’ll talk about collections, but understand that they are all sorts enumerable.

Semantics

It’s a semantic question. You’re going through a collection (array, list, string, dictionary, JSON structure, DB dataset, etc.)? Then the foreach is well suited, he was made for it, he picks up the collection and analyzes item by item, it’s exactly what you want. It’s more convenient.

Security

It is usually safer, robust, reliable since it makes it hard to miss, some errors are impossible in it, and more readable because it indicates what it will do: scan a collection.

Don’t do what you need, conveniently

But it has its idiosyncrasies in it, too. He uses the collection immutably, if that’s what you want, and almost always is, he’s great, but if you need to modify the collection (I’m not talking about her items) there, today (I don’t know in the future), only the for resolves for you.

Another idiosyncrasy is that it takes the value per copy and often you do operations that look like it’s a reference, but it’s not. But already there are structures by reference (ref) that can minimize this.

Has collection that has no order and the foreach is the only way to walk properly.

There are cases that need to do something that the foreach can’t stand it, for example if you need a await in the collection. But it has proposal to accept a foreach await.

If you need scan the collection backwards no way to use the foreach and it seems they don’t want to put a foreachreverse in the language, despite the numerous proposals. Has palliative with cost.

Index

Another very important point is when you need index of the item. Some languages provide the index if you want in the foreach, but most don’t offer it, so if you need it, there’s no way, the for is your solution, it requires the use of an index variable for you to access the item manually. C# does not offer the index, one of the reasons to have the for.

It is possible to create a structure where you pass a collection and receive the value of the item along with the index, then you could use the foreach even in case you need the index, but the performance is worse and gets ugly (even if this is subjective).

using static System.Console;
using System.Linq;

public class Program {
    public static void Main() {
        var lista = new[] { "abc", "def", "ghi" };
        foreach (var item in lista.Select((value, i) => (i, value ))) {
            WriteLine($"{item.i} => {item.value}");
        }
    }
}

Behold working in the .NET Fiddle. And in the Repli.it. I put in the Github for future reference.

Flexibility

The for "pure" is a slightly lower level mechanism and should be avoided but not reneged as many do. It has languages that even have a for, some even the while which has similar but not equal semantics. You can walk in steps in various ways. A for is a while "decked out", just by coincidence he sweeps a collection.

Performance

If you need it and the language offers better performance for that particular case using the for in place of foreach can be more interesting its use. It has to measure, after all if it needs the performance you have already made measurements. It is not to run pro faster without knowing if you need it and which one is faster. In many cases C# is as or faster using foreach than doing manual, it can optimize better by eliminating unnecessary checks because it can prove that it will not access any improper item outside the existing track.

Has cases that the performance of foreach can be much worse, depending on the enumerator used. There are cases that will even create an exponential algorithm unintentionally, and this is tragic. There is nothing trivial difference in a case like this. Of course you need to know how not to enter an O(n2) using the for, is not simple, but at least it allows you to optimize (not in all cases, have problems that are exponential even).

Counting

If what you will sweep is not a collection then the for probably be a more interesting option, after all you are doing a count, you are evolving a sequence, possibly creating a new collection, so it makes no sense to use a foreach.

There’s a proposal for C# ter ranges, there the foreach can be used even when you do not have a collection. Actually this can already be used with Enumerable.Range(), but it’s usually weird to use it in most cases, it seems to me a force to use the foreach where no need. In fact the creation of this method was for use with LINQ which has foreachs internal and needs a solution like this. In the end it didn’t help much when it was implemented, but the person has made some additions to be able to use the crease directly.

Just remember that a for not just to make a count. Many people think only one for (var i = 0; i < c.Length; i++) can be used, but any of the three parts can have almost anything, the first and third are statements, You wait for a boot there, but you can do other things, you can do several initializations, you can do nothing. The third is a step, can put whatever you want to be executed at each step of the repetition, in general it makes no sense to leave blank, but can. The second needs to be a boolean expression and can even be blank, although it is not very suitable. Even this works:

for (;;)

What does it do?

And note that for or foreach are commands and not functions so the "correct" is for () and not for(). Both work the same, but the second seems to be a function, gives the wrong idea.

Completion

Although it works in any case, it has right and has wrong. You travel from Rio to SP by bike, car, truck, bus or plane, each has an advantage, there are guidelines to choose.

2

They both do pretty much the same thing...

They both do the same thing when we think about going through a collection.

So how is it possible to know when to use one or the other?

Depending on your need, the for performs an operation N times, while the foreach performs an operation on all objects/values of the specified collection. Note that for has much more use than foreach, since foreach is basically used to iterate over a collection.

What parameters should I analyze or take into account to decide which may be more advantageous?

Benchmark issue, as you yourself mentioned, will depend on the case where one is more performative than the other.

The foreach should be used in a context to which I will scroll through all objects/values in my collection. The foreach ends up working in any Ienumerable since it uses the Getenumerator() to perform its operation, while the for it is only logical to use in collections to which I can use an index (Ilist arrays and collections).

There are cases of limitation as well. Example is that foreach does not allow you to add or remove items within the collection while you interact on it. If you need to do this, you need to use the is or make a clone of the collection.

1

Generally speaking, there is benchmarks around showing that the difference between them is not so great in most scenarios, but there are some specific ones that have a difference.

Look at this one benchmark for example that shows the interaction of the two commands in various types of objects, and in particular with a DataTable (is still used a lot??? ) the performance of the foreach is superior in the proposed test scenario of course: http://cc.davelozinski.com/c-sharp/for-vs-foreach-vs-while

The other benchmark shows an insignificant difference between the two: https://www.dotnetperls.com/for-foreach

In conclusion, unless you have a very specific scenario, I believe it is more a matter of preference to use one or the other. Of course a loop foreach appears to be more readable, but depends on the type of collection you are using (array, dictionary, list, etc).

Finally, in the Stackoverflow in English, has a legal post on the subject, which addresses other aspects besides performance, which also has other results for comparison: https://stackoverflow.com/questions/365615/in-net-which-loop-runs-faster-for-or-foreach

1

The difference between them is very subtle, which I don’t think will change the performance of your code. But if this level of performance is important to you( we are talking about 2 times or 5 times faster), I advise you to change the language to a lower level. However, it follows here an answer I found in stackoverflow EN.

  • Cycles for are a little more than 2 times cheaper than cycles
  • foreach in Lists. Iterating on a Array is more or less 2 times more economical than iterating on a List.
  • As a consequence, cycles in Array using for is 5 times cheaper than cycles in List using foreach(Which I believe is what we do).

1

Everything depends on your need, I think first comes your need to manipulate/go through that collection. For example:

I always use for when I need to manipulate the objects I’m traveling, because in foreach It is as if a temporary copy of the object was created and if you modify it will not affect the same within the collection. It also has the advantage of knowing which position the object is within the collection. Another case where I use for is when only certain positions need to be covered, in the foreach you can’t because you run from position 0 to X.

I use the foreach in cases where I need to go through the entire list without exception.

These are just some situations that make me decide between one or the other. If both are according to what you need, I think you have to resort to performance, scrolling through a whole list for example can be costly and utilizing the fastest and most efficient may be the best option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.