Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use foreach statement in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the foreach sentence in C#, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

The C # foreach statement is newly introduced in C # and does not exist in C and C++, and Visual Basic programmers should be familiar with it (foreach). It means to collect the elements in a collection and execute embedded statements against each element. The format of the C # foreach statement is:

Foreach (type identifier in expression) embedded-statement

Types (type) and identifiers (identifier) are used to declare loop variables, and expressions (expression) correspond to collections. Each time an embedded statement is executed, the loop variable takes an element in the collection in turn and substitutes it. In this case, the loop variable is a read-only local variable, and if you try to change its value or pass it as a parameter of type ref or out, it will cause a compile-time error.

The expresssion in the C # foreach statement must be a collection type, and if the element type of the collection is not consistent with the loop variable type, there must be an explicit conversion from the element type in the collection to the loop variable element type that is displayed.

I believe everyone is familiar with the concept of set, which represents the general description of a set of identical or similar data items. So what kind of type is a collection type in C #? We syntactically define the collection type:

◆ this type must support a public non-static method in the form of Getenumerator (), whose return type is a structure, class, or interface.

The structure, class, or interface returned by a method in the form of ◆ GetEumerator () should contain a public, non-static method MoveNext () whose return type is Boolean.

The structure, class, or interface returned by a method in the form of ◆ GetEumerator () should contain a public, non-static property, Current, which can be read.

If a type meets the above three conditions, the type is called a collection type. The type of the Current attribute is called the element type of the collection type.

Regardless of the specific form of the collection type, let's just give an example from the point of view of the use of foreach statements.

Suppose Prime is a set type that satisfies the condition, and its element type is a prime number from 0 to 1000. MyInt is a custom type that ranges from 200 to 300 integers. The following program is used to print all prime numbers from 200 to 300 on the screen.

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

By the way, array types support foreach statements. For one-dimensional arrays, the order of execution starts from the element with the subscript 0 to the element of the array; for multidimensional arrays, the increment of the element subscript starts from the rightmost dimension, and so on.

Similarly, break and continue can appear in foreach statements with the same functionality.

The above is all the contents of the article "how to use foreach sentences in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report