Analysis on variables referenced by foreach in C #

Source: Internet
Author: User

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:

 
 
  1. String temp = Name. Text; // read data from textbox
  2. Foreach (lcourse cou in data. mycourse) // traverse in list
  3. {
  4. If (COU. Name = temp) // judge whether the cou name matches
  5. {
  6. Data. mycourse. Remove (COU); // The matched items are to be deleted and removed from the list.
  7. Break; // skip Loop
  8. }
  9. }

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

 
 
  1. foreach(int myint in myArray)  
  2. { 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.

 
 
  1. Public class myclass // custom test class
  2. {
  3. Private int I = 0;
  4. Public void change ()
  5. {
  6. I = 98;
  7. }
  8. Public override string tostring ()
  9. {
  10. Return I. tostring ();
  11. }
  12. }
  13. Myclass [] mytest = new myclass [10]; // test code snippet
  14. Foreach (myclass MC in mytest)
  15. {
  16. MC. Change ();
  17. Console. writeline (MC. tostring ());
  18. }

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.

 
 
  1. List myString = new List();  
  2. myString.Add("ab");   
  3. myString.Add("cd");   
  4. foreach (string s in myString)   
  5. {   
  6. myString.Remove(s);   
  7. }  

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.

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.