As an addition to what everyone else has written, I want to write a bit of theory.
for
: It is a cycle that iterates within all a set of elements, either a list, or a Array
(Generic example ) :
int[] Array = { 5, 4, 3, 2, 1 };
for (int i = 0; i < Array.Length; i++)
Console.Write(Array[i] + ", ");
In each of your iterations you will find the value of the i
element in Array
, causing the following result:
5, 4, 3, 2, 1,
As you can see, yes, each element is actually accessed within Array
.
With foreach
this could be translated to:
foreach (int e in Array)
Console.Write(e + ", ");
Which would produce the same output, now, we are talking about Array
, an array is only a set of finite elements, if we are oriented to a List<T>
, things change:
In a class whose main dependency is an iterator, why not use foreach
to access each element of List<T>
? If we mention the performance, we are in nothing, the class List
is ready for "fire" in the .NET Framework and the cycle foreach
also, but, you can test the speed in each one . (Another generic example) :
public static void Main()
{
List<int> Lista = new List<int>();
Stopwatch S = new Stopwatch();
Console.WriteLine("Iniciando cronometro para llenar la lista con 10,000 valores");
S.Start();
for (int i = 0; i < 10000; i++) Lista.Add(i);
S.Stop();
Console.WriteLine("Tiempo pasado llenar la lista: " + S.Elapsed.ToString());
Console.WriteLine("\n\nIniciando cronometro para acceder a cada elemento usando el ciclo for:");
S.Restart();
for (int i = 0; i < Lista.Count; i++)
Console.Write(Lista[i]);
S.Stop();
Console.WriteLine("\nTiempo transcurrido usando for: " + S.Elapsed.ToString());
Console.WriteLine("\nIniciando cronometro para acceder a cada elemento usando foreach: ");
S.Restart();
foreach (int e in Lista)
Console.Write(e);
S.Stop();
Console.WriteLine("\nTiempo transcurrido usando foreach: " + S.Elapsed.ToString());
}
(Run the previous example several times to check the results obtained)
Now, if what you want is to obtain the index of an element in a class that contains iterators or supports index
, it is best to use your indexer or call its respective function IndexOf
(As they mention Asier Villanueva and rsciriano )
IndexOf
: It is a function that returns an integer with the current position of an element in your list, it is worth mentioning that this function is only available for the collections that are in the namespace System.Collections.Generic
, its appearance is more or less like this:
int IndexOf<T>(this T[] item, T toSearch)
{
for (int i = 0; i < item.Length; i++)
if (item[i] == toSearch) return i;
return -1; // No recuerdo si arroja al no encontrarlo, por lo que devolvemos null.
}
That is practically the same as iterating the list or collection to obtain the index of the current element, the practical uses of this function, come when you need to remove a specific element, example generic :
IList<string> Nombres = new List<string>() {
"NaCl", "Miquel", "Asier", "Op", "Etc.."
};
And I need to specifically take my name, if it exists:
int IndexDeOp = Nombres.IndexOf("Op");
The value of IndexDeOp
is 3
, so if you do not need to iterate within the list, this is your best option.
As a last mention, the cycle foreach
can only be used is used in classes or "collections" that implement IEnumerable[<T>]
, but this is not entirely necessary, it is enough that the following functions and properties are implemented: GetEnumerator
, Current
and MoveNext
.
Here are some reference links:
for
, foreach
, a fiddle .