The foreach statement provided by C# language is a shortcut to a for statement loop, and it also promotes more consistency of collection classes. Let’s first look at its definition format:
The definition format of the foreach statement is:
foreach(type variable in collection)
{
Sub-statement;
}
Every time an embedded statement is executed, the loop variable takes an element in the set and substitutes it into it. Here, the loop variable is a read-only local variable. If you try to change its value, a compilation error will occur.
The foreach statement is used to enumerate all elements in the set. The expression in the foreach statement is composed of two items separated by the keyword in. The item on the right of in is the collection name, and the item on the left of in is the variable name, which is used to store each element in the set.
Advantages of foreach statement:The sentences are concise and efficient.
Use an example of traversing array elements to illustrate:
Use firstThe foreach statement outputs elements in the array:
int[,] ints =new int[2,3]{{1,2,3},{4,5,6}}; foreach (int temp in ints) { (temp); } ();
Use againThe for statement outputs elements in the array:
int[,] ints =new int[2,3]{{1,2,3},{4,5,6}}; for (int i = 0; i < (0); i++) { for (int j = 0; j < (1); j++) { (ints[i,j]); } } ();
The result of these two codes is the same. Each line has an element, with a total of 6 lines, and the elements are 1 2 3 4 5 6 respectively.
The simplicity and efficiency of foreach statements cannot be reflected in one-dimensional arrays, but it is more obvious and convenient in two-dimensional arrays and even multi-dimensional arrays. Therefore, in C# language, it is recommended to use foreach statements.
Advantages of foreach statements:Avoid unnecessary factors
When using foreach statements in C#, you don’t need to consider what the array start index is. Many people may transfer from other languages to C#. Then the original language start index may not be 1, such as VB or Delphi. When using arrays in C#, it is inevitable to ask whether to start with 0 or 1, so foreach can avoid such problems.
forAdvantages of each statement:Foreach statement automatically completes type conversion
This embodiment may not show any effect through the examples above, but for data sets such as ArrayList, this operation is more prominent.
Use firstForeach statement to implement type conversion operation: when using the ArrayList class, you must first introduce using;
int[] a=new int[3]{1,2,3}; ArrayList arrint = new ArrayList(); (a); foreach (int temp in arrint) { (temp); } ();
Come againUse the for statement to implement: explicit casting is required
int[] a=new int[3]{1,2,3}; ArrayList arrint = new ArrayList(); (a); for (int i = 0; i < ;i++ ) { int n = (int)arrint[i]; (n); } ();
The output results of the two programs are: one element per line, 1, 2, and 3 respectively.
The foreach statement is even more concise for string classes:
using System; using ; using ; using ; using ; namespace @foreach { class Program { static void Main(string[] args) { string str = "This is an example of a foreach"; foreach (char i in str) { if ((i)) { (i);// When i is space output and line break } else { (i);// When i is not a space, it is just output } } (); } } }
The output result is: each line has a word, which is This, is, an, example, of, a, foreach.
I know that this is more important to understand foreach statements. With deeper learning, you may have a better understanding.
The above is all about this article, I hope it will be helpful to everyone's learning.