1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Text ;
5
- using System . Threading . Tasks ;
6
2
7
3
namespace LISTING_1_57_uppercase_Person
8
4
{
5
+ /*
6
+
7
+ If the foreach loop is working on a list of references to objects, the objects on the ends of those references can
8
+ be changed.
9
+
10
+ The foreach construction can iterate through any object which implements the IEnumerable interface. These objects
11
+ expose a method called GetIterator(). This method must return an object that implements the
12
+ System.Collections.IEnumerator interface. This interface exposes methods that the foreach construction can use to
13
+ get the next item from the enumerator and determine if there any more items in the collection. Many collection
14
+ classes, including listst and dictionaries, implement the IEnumerable interface.
15
+
16
+ Note that the iteration can be implemented in a "lazy" way; the next item to be iterated only needs to be fetched
17
+ when requested. The result of database queries can be returned as objects that implement the IEnumerable interface
18
+ and then only fetch the actual data items when needed. It is important that the item being iterated is not changed
19
+ during iteration, if the iterating code tried to remove items from the list it was iteraqting through this would
20
+ cause the program to throw an exception when it ran.
21
+
22
+ */
9
23
class Program
10
24
{
11
25
class Person
@@ -20,18 +34,24 @@ public Person(string name)
20
34
21
35
static void Main ( string [ ] args )
22
36
{
23
- Person [ ] people = new Person [ ] {
37
+ Person [ ] people = new Person [ ]
38
+ {
24
39
new Person ( "Rob" ) , new Person ( "Mary" ) ,
25
40
new Person ( "David" ) , new Person ( "Jenny" ) ,
26
- new Person ( "Chris" ) , new Person ( "Imogen" ) } ;
41
+ new Person ( "Chris" ) , new Person ( "Imogen" )
42
+ } ;
27
43
28
44
foreach ( Person person in people )
45
+ {
29
46
person . Name = person . Name . ToUpper ( ) ;
47
+ }
30
48
31
49
foreach ( Person person in people )
50
+ {
32
51
Console . WriteLine ( person . Name ) ;
52
+ }
33
53
34
54
Console . ReadKey ( ) ;
35
55
}
36
56
}
37
- }
57
+ }
0 commit comments