C # does not allow changing the values of elements in an array or collection in a Foreach Loop (note: The value of a member is not affected)

Source: Internet
Author: User

C # does not allow the values of elements in the array or collection to be altered in the Foreach Loop (note: The value of the member is not affected), as the following code will not compile.

(in MyArray){x//error code because the value of the element was changed by the Console.  WriteLine(x);}         

If you want a custom data type to support a foreach loop, the type must implement the Ienumerable<t> interface, and there is an ienumerator<t> implementation for this list. In fact, at the bottom of. NET (Il language level),

(in list//x is of type T... }

Equivalent to the following code:

(= List. ) The type of GetEnumerator ()//iterator is ienumerator<t>(iterator.  MoveNext(= iterator.  Current()}}              

It is just an equivalent descriptive form to make it easier for users to use.

C # does not allow changing the values of elements in an array or collection in a Foreach Loop (note: The value of a member is not affected)

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.