It is very often that we have to make some operations on every element in the collection. The most popular ways of doing this is to use a “for” or ”foreach” loop. Here are three ways of doing this:
- “For” loop – One of the oldest loops known to the mankind. It is the easy solution to get e.g. particular element of the array, but it is not the most efficient way of iterating over collections.
- “foreach” loop – This is a newer type of loop which iterates over the whole collection of objects. It is easy, it is fast and you can use it on every ICollection and array.
-
"ForEach" – Public method of the generic list which, from the definition: Performs the specified action on each element of the System.Collections.Generic.List
. This is considered as the fastest way of doing some operations on every element of the generic List.
At first it seems that the classic “foreach” and generic List “ForEach” are the same, there are some significant differences. Here is a very simple example of them suing the most popular collection: generic list.
Let’s create a list of strings and populate it with come Guids:
ListlistOfStrings = new List (); for (int i = 0; i < 1000000; i++) { listOfStrings.Add(Guid.NewGuid().ToString()); }
Now initialize a new list which will be holding changed values from our first list:
ListlistOfNewStrings = new List ();
And now we will do some operations on each element of the collection using classic foreach:
foreach (string s in listOfStrings) { if (s.Contains("6")) continue; else if (s.Contains("4")) return; else listOfNewStrings.Add(string.Format(s + "{0}", "SUFIX")); }
Everything is ok, the code is compiling and now we will do the funny part. We will make some operations on every element of the list using the generic list ForEach method:
listOfStrings.ForEach(s => { if (s.Contains("6")) continue; else if (s.Contains("4")) return; else listOfNewStrings.Add(string.Format(s + "{0}", "SUFIX")); });
And here is the surprise- the code is not compiling. We do the same operations on the same list so what is going on?
The answer is quite simple: ForEach from the System.Collections.Generic.List
ForEach, as an argument, takes the System.Action
The advantages of using “ForEach” are the ease of use, readability and ForEach can modify the list over which it is iterating, so if we do not need to use break or continue inside the loop, we can use the ForEach on the generic List.
0 comments:
Post a Comment