7
I have a list of several string
, there’s a difference between going through the list values:
In this way:
ListaString.ForEach(delegate(string str)
{
Console.WriteLine(str);
});
Or this:
foreach(string str in ListaString)
{
Console.WriteLine(str);
}
Is one more performatic than the other? Is there any significant gain?
When recommended to use list.foreach
? and foreach
? in the case of List<T>
.
Note: I know that the Foreach
can be used to browse arrays
and other things.
But I want to know specifically these two ways put up in situations that involve List<T>
.
Because it is an extension method (System.Linq) I believe that internally it should call the common foreach or something similar, so the performance should be very similar. As for the use, I believe it will go well. If it’s something simple, a simple call or a concatenation, I think the list.Foreach is more beautiful and simple to read.
– Caique C.