Easily learn the foreach iterative statement of C

  • 2021-08-21 21:15:08
  • OfStack

The foreach statement provided by the C # language is a shortcut to an for statement loop, and it also promotes a more uniform collection class. Let's take a look at its definition format first:
The foreach statement is defined in the following format:
foreach (Type Variable in Collection)
{
Substatement;
}
Every time an inline statement is executed, the loop variable is substituted in one element of the collection in turn. In this case, the loop variable is a read-only local variable, and if you try to change its value, a compilation error will occur.
The foreach statement is used to enumerate all the elements in the collection, and the expression in the foreach statement consists of two items separated by the keyword in. The item to the right of in is the collection name, and the item to the left of in is the variable name, which is used to hold each element in the collection.
Advantages of foreach statement 1: The statement is concise and efficient.
Use an example of traversing array elements to illustrate:
First, use the foreach statement to output the elements in the array:


<span style="font-size:18px;">      int[,] ints =new int[2,3]{{1,2,3},{4,5,6}}; 
      foreach (int temp in ints) 
      { 
        Console.WriteLine(temp);  
      } 
      Console.ReadLine();</span> 

Then use the for statement to output the elements in the array:


<span style="font-size:18px;">   int[,] ints =new int[2,3]{{1,2,3},{4,5,6}}; 
 for (int i = 0; i < ints.GetLength(0); i++) 
 { 
   for (int j = 0; j < ints.GetLength(1); j++) 
   { 
     Console.WriteLine(ints[i,j]); 
   } 
 } 
 Console.ReadLine();</span> 

The result of these two types of code execution is one element per line for six lines, and the elements are 1 2 3 4 5 6 respectively.
The simplicity and efficiency of foreach statement can not be shown in 1-dimensional array, but it is more obvious and convenient in 2-dimensional array or even multi-dimensional array, so it is recommended to use foreach statement with loop statement in C # language.
Advantage 2 of foreach statement: Avoid unnecessary factors
In C # language using foreach statement does not need to consider the initial index of the array is several, many people may turn from other languages to C #, then the initial index of the original language may not be 1, for example, VB or Delphi language, then in C # when using arrays, it is inevitable to question whether to use 0 or 1, so foreach can avoid such problems.
Advantage 3 of foreach statement: foreach statement automatically completes type conversion
This embodiment may not have any effect from the above example, but for data sets such as ArrayList, this operation is more prominent.
First, use foreach statement to realize type conversion operation: When using ArrayList class, we should first introduce using System. Collections;


<span style="font-size:18px;">      int[] a=new int[3]{1,2,3}; 
      ArrayList arrint = new ArrayList(); 
      arrint.AddRange(a); 
      foreach (int temp in arrint) 
      { 
        Console.WriteLine(temp); 
      } 
      Console.ReadLine();</span> 

Then use the for statement to realize it: explicit casting is required


<span style="font-size:18px;">      int[] a=new int[3]{1,2,3}; 
      ArrayList arrint = new ArrayList(); 
      arrint.AddRange(a); 
      for (int i = 0; i < arrint.Count;i++ ) 
      { 
        int n = (int)arrint[i]; 
        Console.WriteLine(n); 
      } 
      Console.ReadLine();</span> 

The output result of the two programs is: 1 element per 1 line, which is 1, 2 and 3 respectively.
The foreach statement is even more concise for the string class:


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace @foreach 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      string str = "This is an example of a foreach"; 
      foreach (char i in str) 
      { 
        if (char.IsWhiteSpace(i)) 
        { 
          Console.WriteLine(i);// When i Output and wrap when it is a space  
        } 
        else 
        { 
          Console.Write(i);// When i Only output when it is not a space  
        } 
      } 
      Console.ReadLine(); 
    } 
  } 
}</span> 

The output result is: 1 word per line, which is This, is, an, example, of, a and foreach respectively.
As for the understanding of foreach statement, we know more about it at present. With deeper learning, we may have a better understanding.

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: