Hi,
I have 2 classes and in the Persons
class I want to add the ability of looping through the name properties of the Person collection using a foreach loop, like this:
foreach (string name in Persons.Names)
{
// do something
}
How would I do this?
These are my classes:
class Person
{
public string Name
{
get;
set;
}
public string Surname
{
get;
set;
}
}
class Persons : IEnumerable<Person>
{
List<Person> persons = new List<Person>();
public IEnumerator<Person> GetEnumerator()
{
foreach (Person p in persons)
{
yield return p;
}
}
System.Collections.IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<string> Names
{
// implement looping through Name property of Person class
}
public Persons()
{
}
public void Add( Person p )
{
persons.Add(p);
}
public string this[int index]
{
get { return this[index]; }
}
}