This article introduces that in C #, The foreach reference variable is probably directed to the following set through the address. When the element of the set is modified, the address may change in a way, so the loop does not jump out and continues the time, the reference may not be found.
AD:
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, and foreach is a new tool for traversing. Naturally, it is used. The code is similar to the following:
- 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?
In C #, foreach references the variable 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 examples at the beginning, I seem to have seen something about foreach.
First, you cannot modify the foreach reference variable in C # 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 in C # Is probably directed to the following set through the address (I guess it is similar to the pointer of C/C ++). 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, 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.