Making a Class Based on Arrays Enumerable in C#
Okay, I have made an earnest effort to understand this over the past hour
or so. So I am wondering if someone can explain this to me.
I'm trying to make a class in C# be Enumerable. Specifically, I'm trying
to make it work with a foreach loop. I have a test going with a simple
class, with takes in characters into the constructor.
EmployeeArray ArrayOfEmployees = new EmployeeArray('a','b','c');
foreach(char e in EmployeeArray) //Nope, can't do this!
{
Console.WriteLine(e);
}
//---Class Definition:---
class EmployeeArray
{
private char[] Employees;
public EmployeeChars(char[] e)
{
this.Employees = e;
}
//Now for my attempt at making it enumerable:
public IEnumerator GetEnumerator(int i)
{
return this.Employees[i];
}
}
No comments:
Post a Comment