C# Chapter 13
C# Chapter 13
Collection Objects
Earlier we used an array in a foreach. If the array had three members, the foreach got executed
three times and each time the variable i had a different value. The concept described below is
called a collection class. It is a class that returns a value each time till the values run out, thus
making it easier for us to iterate through the array.
a.cs
foreach (string i in f)
class yyy
Compiler Error
a.cs(6,1): error CS1579: foreach statement cannot operate on variables of type ‘yyy’ because
‘yyy’ does not contain a definition for ‘GetEnumerator’, or it is inaccessible
a.cs
foreach (string i in f)
class yyy
Compiler Error
a.cs(6,1): error CS1579: foreach statement cannot operate on variables of type 'yyy' because 'int'
does not contain a definition for 'MoveNext', or it is inaccessible
a.cs(13,12): error CS0161: 'yyy.GetEnumerator()': not all code paths return a value
Foreach obviously tries to execute the function called GetEnumerator. This function should not
return an int but something else as the error suggests.
a.cs
using System.Collections;
foreach (string i in f)
class yyy
}
Compiler Error
IEnumerator is an interface which has three functions MoveNext, Reset and Current and xxx has
to implement all of them to remove the compiler errors.
a.cs
using System.Collections;
foreach (string i in f)
System.Console.WriteLine(i);
class yyy
System.Console.WriteLine(“MoveNext”);
return true;
System.Console.WriteLine(“Reset”);
get
System.Console.WriteLine(“Current”);
return “hi”;
}
}
Run the program and you will notice that the output does not stop. It goes on forever.
a.cs
using System.Collections;
foreach (string i in f)
System.Console.WriteLine(i);
class yyy
{
return new xxx();
i++;
System.Console.WriteLine(“MoveNext” + i);
if ( i == 3)
return false;
else
return true;
System.Console.WriteLine(“Reset”);
get
{
System.Console.WriteLine(“Current “ + a[i]);
return a[i];
Output
MoveNext0
Current hi
hi
MoveNext1
Current bye
bye
MoveNext2
Current no
no
MoveNext3
We have created an array a which has 3 members and initialized them respectively to hi, bye and
no by giving the strings in {} immediately after the new. Each time MoveNext gets called the
variable i is increased by 1. If the value of i is 3, we have no more strings to return and thus we
return false, else we return true. The variable i keeps track of how many times the function
MoveNext is being called. As MoveNext returns true, Current gets called which returns a string
from the array using i as the offset. Thus we can iterate through the entire array depending upon
the length.
a.cs
using System.Collections;
public class zzz
foreach (string i in f)
System.Console.WriteLine(i);
class yyy
string t;
t = t1;
}
class xxx : IEnumerator
public string [] a;
b[0] = ‘ ‘;
a = t3.Split(b);
i++;
System.Console.WriteLine(“MoveNext “ + i);
if ( i == a.Length)
return false;
else
return true;
System.Console.WriteLine(“Reset”);
}
public object Current
get
System.Console.WriteLine(“Current “ + a[i]);
return a[i];
Output
MoveNext 0
Current This
This
MoveNext 1
Current is
is
MoveNext 2
Current Great
Great
MoveNext 3
Pretty big program. At the time of creating a yyy object we are passing a string to the
constructor. Thus the yyy constructor gets called first. The constructor stores this string in
variable t. The foreach statement calls GetEnumerator which now creates a xxx object passing it
the string through t. The constructor of xxx now gets called. Every string class has a member
function called Split. Split will break up a string on certain characters which we call delimiters.
In this case, we want our string to be broken up whenever we encounter a space. The Split
function requires an array of chars which it can use as a delimiter. The reason it requires an array
is because we may have more than one char that we would like to break the string on. Like
earlier, the array a now contains the array of strings. The last change is the condition in the if
statement. Earlier we used a constant number, now we use a member, Length, of an array which
stores the length of the array or the number of members. Thus the class yyy can now be used as a
collection class which enumerates the individual words in the string. The function Reset for
some reason never ever gets called.