C #-foreach

Source: Internet
Author: User

Introduction:

I did my teacher's website homework yesterday. Add, modify, and delete objects. Nothing else. A problem occurs during deletion.

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

StringTemp = Name.Text;// SlaveTextboxReading data
Foreach(Lcourse couInData. mycourse)// InList <lcourse>Traverse
{
If(COU. Name = temp)// JudgmentCouName match
{
Data. mycourse. Remove (COU );// The matched items are to be deleted and removed from the list.
Break;// Skip Loop
}
}

It's easy to think of this.Foreach, It seems that there is no error. HoweverForeachThe note is that it is only applicable to reading through traversal and cannot be modified. The above code is deleted successfully. In this way, the seeds of contradictions are planted in the heart, to the endForeachWhat is it?

Process:

ReadMsdnDocument,But the example in the document is usedForeachEvery element is read through traversal, so it is not helpful. Related tests, exceptions, and compilation errors are quite useful.

Process details and summary:

1, Custom integer array, and then traverse and modify

Foreach(IntMyintInMyarray)

{Myint + = 8;}

Error1 "Myint"Is"ForeachIteration variable", Cannot assign value to itG: \ test \ vs .. C # \ testforeach \ Program. CS 15 27 testforeach
2, Custom class, define functions in the class to operate on Private Members, then traverse class objects and call Functions

Public ClassMyclass// Custom test class
{
Private intI = 0;
Public voidChange ()
{
I = 98;
}
Public override stringTostring ()
{
ReturnI. tostring ();
}
}

Myclass [] mytest =NewMyclass [10];// Test code snippet
Foreach(Myclass MCInMytest)
{
MC. Change ();
Console. writeline(MC. tostring ());
}

Compiled, but an exception is thrown,NullrefferenceexceptionObject reference is not set to the instance of the object.

3Is an example of testing at the beginning. It is slightly changed and is not needed after removal.BreakJump out

List <String> Mystring =NewList <String> ();

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,InvalidoperationexceptionThe set has been modified. enumeration may not be performed.

Through the above three test examples and the actual examples at the beginning, I seem to have seenForeachSomething.

First, what cannot be modified isForeachReference a variable because it is referenced and does not make any changes to it. It is likeC/C ++The Pointer Points to the element in the set as a reference; we can see it through the reference, but it is not capable of moving it.

Second,InThe previous variables are referenced variables,InThe subsequent collection is real, so you can operate on it, like. Add (); remove ()And other methods.

Third,ForeachThe referenced variable is probably directed to the following set through the address (I guessC/C ++When the elements of the set are modified, the address may change. Therefore, if you do not jump out of the loop and continue the traversal, the reference may not be found.

Finally, it may be inaccurate, but this is all I understand. First of all, I would like to praise my own ideas and spirit. As for the depth of technology, there is almost nothing. Make persistent efforts.

 

 

@ Yitian Studio

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.