Quantcast
Channel: 2,000 Things You Should Know About C# » foreach
Browsing latest articles
Browse All 10 View Live

Image may be NSFW.
Clik here to view.

#1,115 – Iterating Through a Collection Using the foreach Statement

In the same way that you can iterate through an array using foreach, you can iterate through the elements of a collection one at a time.  You do this by writing a loop–a block of code that is executed...

View Article



Image may be NSFW.
Clik here to view.

#1,116 – Iterating Through a String Using the foreach Statement

You can iterate through the individual characters in a string using the foreach statement.  The iteration variable is of type char and is set consecutively to each character within the string as the...

View Article

Image may be NSFW.
Clik here to view.

#1,117 – foreach Requires IEnumerable Implementation

The foreach statement works on an object that implements IEnumerable<T> or IEnumerable.  It also works on an object whose type has a public GetEnumerator method that returns an...

View Article

Image may be NSFW.
Clik here to view.

#1,118 – foreach Works with Iterator Returned by yield

You can use the yield keyword in a method to implement an iterator.  The method in which it is defined returns an IEnumerable or IEnumerator.  The resulting iterator can be consumed using the foreach...

View Article

Image may be NSFW.
Clik here to view.

#1,119 – Scope of Iteration Variable Is Limited to Body of foreach

The iteration variable that is set to a new element during each pass of a foreach statement only has scope within the body of the foreach loop.  It may not be used or referenced outside of the loop....

View Article


Image may be NSFW.
Clik here to view.

#1,120 – The Iteration Variable in a foreach Loop Is Read-Only

Unlike the for loop, the iteration variable in a foreach loop is read-only.  Attempting to assign a value to the variable within the body of the loop will result in a compile-time error. string[] dogs...

View Article

Image may be NSFW.
Clik here to view.

#1,121 – Modifying Elements in a Collection Using foreach

Although the iteration variable in a foreach loop is read-only, you can still use the iteration variable to modify the elements of a collection, as long as they are reference-typed.  (Value-typed...

View Article

Image may be NSFW.
Clik here to view.

#1,122 – It’s Okay to Capture Variables within a foreach Loop

Within a foreach loop, you capture an iteration variable when you save it for later use.  In versions of C# earlier than 5.0, this led to problems and unexpected behavior.  What ended up being captured...

View Article


Image may be NSFW.
Clik here to view.

#1,123 – Using foreach to Iterate on a Multidimensional Array

You can use the foreach statement to iterate through all elements in a multidimensional array.  The elements are iterated in a row-major order.  That is, all elements in the first row are enumerated,...

View Article


Image may be NSFW.
Clik here to view.

#1,124 – Iterate through Jagged Array with Nested foreach

You can’t use the foreach statement to directly iterate over all elements in a jagged array.  You can, however, iterate through the jagged array using nested foreach statements.  Each foreach statement...

View Article
Browsing latest articles
Browse All 10 View Live




Latest Images