1 for a given length does not need to calculate the length of the for than the Foreach Loop efficiency high
2 It is convenient to use foreach in the uncertain length or when the calculation length has a performance loss
The loop statement is the BASIC programming statement, in C # In addition to the C language of the loop statement, but also provides a foreach statement to implement the loop. So what I'm going to say is to use the foreach statement as much as possible in the loop operation.
To better illustrate why you should advocate using foreach, write loop statements in the following three different ways.
int[] NArray = new int[100];
Use the "foreach" to Loop array
foreach (int i in NArray)
Debug.WriteLine (i.ToString ());
Use ' for ' to loop array
for (int i = 0; i < narray.length; i++)
Debug.WriteLine (Narray[i]. ToString ());
Another-to-loop array with using "for"
int nlength = Narray.length;
for (int i = 0; i < nlength; i++)
Debug.WriteLine (Narray[i]. ToString ());
Obviously, the foreach statement is concise, but its advantages are not only in this, it is also the highest efficiency. Many people may think that the last one is more efficient because it looks as if you don't have to access the properties of the reference type every time. But it is among the three, the least efficient. Because C # is a strong-type check, when it comes to array access, the valid values of the index are judged, and the final code actually produces the same effect as the following code.
Another-to-loop array with using "for"
int nlength = Narray.length;
for (int i = 0; i < nlength; i++)
{
if (I < narray.length)
Debug.WriteLine (Narray[i]. ToString ());
Else
throw new IndexOutOfRangeException ();
}
(There are some discrepancies in the book, after netizens Sozdream hint, in 1.1 environment found the last method is the fastest, the first two of the speed is basically equal; The code generated by dissambly to view the last loop method does not produce an index check similar to what the article says. However, it is not recommended to use the last one, because this method is somewhat disjointed to the evaluation of the index, especially when the size of the array in the loop changes, the index effective check cannot be done in time.
In addition to being concise and efficient, the foreach statement has many advantages, which are listed next.
The first is not to consider the array starting index is a few, many people may be transferred from other languages to C #, then the original language of the starting index may not be 1, such as VB or Delphi language, then the use of arrays in C # when the question of whether to use the 0 start or 1 start? Using foreach, you can avoid this kind of problem.
The second benefit is that it is very easy to use foreach for multidimensional array operations, such as:
int[,] nvisited = new int[8,8];
Use ' for ' to loop two-dimension array
for (int i = 0; i < nvisited.getlength (0); i++)
for (int j = 0; J < nvisited.getlength (1); j + +)
Debug.WriteLine (Nvisited[i,j]. ToString ());
Use ' foreach ' to loop two-dimension array
foreach (int i in nvisited)
Debug.WriteLine (i.ToString ());
For three-dimensional or more dimensions, the foreach statement does not have to change, and for the for statement to be modified, there is not much to say.
The third thing to say is that the foreach completes the type conversion operation, which may not show any effect through the example above, but for datasets such as ArrayList, this is a more prominent operation, such as:
Init an ArrayList object
int[] NArray = new int[100];
ArrayList arrint = new ArrayList ();
Arrint.addrange (NArray);
Use "foreach" to Loop an ArrayList
foreach (int i in arrint)
Debug.WriteLine (i.ToString ());
Use ' for ' to loop an ArrayList
for (int i = 0; i < Arrint.count; i++)
{
int n = (int) arrint[i];
Debug.WriteLine (N.tostring ());
}
The last thing to say is that using foreach does not increase the use of resources, which is a bit difficult to understand because the foreach statement can be used because of the type data that inherits the IEnumerable interface. So for enumerations that use foreach to access the GetEnumerator method in the IEnumerable interface, the corresponding statement for the foreach statement above is as follows:
IEnumerator it = Arrint.getenumerator () as IEnumerator;
using (IDisposable disp = it as IDisposable)
{
while (it. MoveNext ())
{
int elem = (int) it. Current;
Debug.WriteLine (Elem. ToString ());
}
}
This means that the IEnumerator object is disposed after foreach.
There are so many benefits to foreach that the use of it can be impeccable. In fact, there are two restrictions in the foreach statement, first you cannot modify the enumeration members, and then do not delete the collection. That is, the following two ways are wrong.
Use "foreach" to Loop an ArrayList
foreach (int i in arrint)
{
I++;//can ' t be compiled
Debug.WriteLine (i.ToString ());
}
Use "foreach" to Loop an ArrayList
foreach (int i in arrint)
{
Arrint.remove (i);//it'll generate error in Run-time
Debug.WriteLine (i.ToString ());
}
So for the above two operations, you can use for to achieve, in addition to say here, that is, for a record set of multiple data deletion problem, is also a frequent problem of the place (the forum often ask a similar question), because in some recordsets delete, the corresponding index has changed after the delete operation, This time the deletion to be deleted in turn, the approximate form is as follows.
Use ' for ' to loop an ArrayList
for (int i = arrint.count-1; I >=0; i--)
{
int n = (int) arrint[i];
if (n = = 5)
Arrint.removeat (i); Remove Data here
Debug.WriteLine (N.tostring ());
}
In addition to these two places, foreach can be basically applied to any loop, so use foreach as much as possible for the writing of loops, because it will make your code clear, concise, and yet efficient.
C # for and foreach