C # 's foreach statement

Source: Internet
Author: User
Tags foreach expression

The foreach statement was introduced in C #, and there was no such statement in C and C + +, and the Visual Basic programmer should not be unfamiliar with it. It represents the collection of individual elements in a collection and executes inline statements for each element. The format of the statement is:

foreach (type identifier in expression) embedded-statement

where types (type) and identifiers (identifier) are used to declare loop variables, and expressions (expression) correspond to collections. Each time an inline statement is executed, the loop variable takes one of the elements in the collection into it. Here, the loop variable is a read-only local variable that throws a compile-time error if you try to change its value or pass it as a parameter of a ref or out type.

The expresssion in a foreach statement must be a collection type, and if the element type of the collection is inconsistent with the loop variable type, you must have an explicit conversion that displays the defined element type from the collection to the type of the loop variable element.

The concept of a set is not unfamiliar to anyone, it represents the total description of a set of identical or similar data items. So what kind of type is a collection type in C #? We give the definition of the collection type from the syntax:

The type must support a publicly non-static method that is GetEnumerator (), and the return type of the method is a struct, class, or interface.

The struct, class, or interface returned by the Geteumerator () method should contain a publicly non-static method, MoveNext (), and the return type of the method is a Boolean.

The structure, class, or interface returned by a method of Geteumerator () should contain a publicly non-static property current, which can be read.

If a type satisfies the above three conditions, the type is called a collection type. The type of the current property is called the element type of the collection type.

Let's just give an example of the use of a foreach statement, regardless of the specific form of the collection type.

Suppose Prime is a collection type that satisfies a condition, and its element type is a prime number within 0 to 1000. Myint is our custom type, which ranges from 200 to 300 integers. The following procedure is used to print out all prime numbers from 200 to 300 on the screen.

Program Listing 8-5:

Using System;
Using System.Collections;
Class Test
{public
  static void Main ()
  {
    Console.WriteLine ("Prime number:");
    foreach (MyInt x in Prime)
       Console.WriteLine ("{0}", x);
  }
}

By the way, the array type supports the foreach statement, and for a one-dimensional array, the order of execution begins with the element subscript 0, to the last element of the array, and for multidimensional arrays, the increment of the subscript for the element starts from the rightmost dimension, and so on.

Similarly, break and continue can appear in a foreach statement with no change in functionality.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.