C # foreach statements

Source: Internet
Author: User

C # foreach statements are often inaccurate for beginners of C. The following example shows the errors in the C # foreach statement, finds out the errors, and summarizes the usage process of the foreach statement. It is helpful for you to learn the C # foreach statement.

C # foreach statement example description:

To add, delete, modify, and delete the content in the Set, traverse the set. The emergence of the C # foreach statement solves this problem.

This is because an element is deleted from a collection of classes. In this way, we need to traverse the entire set, and foreach is a new tool for traversing. Naturally, it is used. ThereforeCodeFor example: 

String temp = Name. Text; // read data from textbox

Foreach (lcourse cou in data. mycourse) // traverse in list

{

If (COU. Name = temp) // judge whether the cou name matches

{

Data. mycourse. Remove (COU); // The matched items are to be deleted and removed from the list.

Break; // skip Loop

}

}

It is easy to think that using foreach seems to be okay. However, note that foreach is only applicable to traversal and reading and cannot be modified. The above code is deleted successfully. In this way, the seeds of contradictions are planted in our hearts. What is foreach?

C # foreach statement learning and analysis process:

I have read the msdn document, but the examples in this document are used to traverse and read each element, which is of little help. Related tests, exceptions, and compilation errors are quite useful.

Process details and summary:

1. Customize the integer array and traverse and modify it

Foreach (INT Myint in myarray)

{

Myint + = 8;

}

Error: "Myint" is a "foreach iteration variable" and cannot be assigned with G: \ test \ vs. C # \ testforeach \ Program. CS 15 27 testforeach

2. Custom class: Define a function in the class to operate on Private Members, traverse class objects, and call functions.

Public class myclass // custom test class

{

Private int I = 0;

Public void change ()

{

I = 98;

}

Public override string tostring ()

{

Return I. tostring ();

}

}

Myclass [] mytest = new myclass [10]; // test code snippet

Foreach (myclass MC in mytest)

{

MC. Change ();

Console. writeline (MC. tostring ());

}

The compilation is successful, but an exception is thrown. nullrefferenceexception indicates that the object reference is not set to the instance of the object.

3. This is an example of testing at the beginning. It is slightly changed. after removal, you do not need to use break to jump out.

List mystring = new list ();

Mystring. Add ("AB ");

Mystring. Add ("cd ");

Foreach (string s in mystring)

{

Mystring. Remove (s );

}

The compilation is successful, but an exception is thrown during runtime. invalidoperationexception indicates that the set has been modified. enumeration may not be performed.

Through the above three test examples and the actual example at the beginning, it is actually a description of something in the C # foreach statement.

First, what cannot be modified is that foreach references the variable because it is referenced and does not make any changes to it, it is like a C/C ++ pointer that points to reference the elements in the set; we can see it through reference, but it is not capable of moving it.

Second, the variable before in references the variable, but the set after in is real, so you can operate on it, such as. Add (); remove () and other methods.

Third, the foreach reference variable is probably directed to the following set through the address (I guess it is similar to the C/C ++ pointer). When the element of the set is modified, the address may change, so the reference may fail to be found without jumping out of the loop.

Finally, the usage of C # foreach statements is not accurate, but it is helpful to understand and learn C # foreach statements.

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.