title: Implementing IEnumerable question
body: <p>I have 2 classes and in the <code>Persons</code> class I want to add the ability of looping through the name properties of the Person collection using a foreach loop, like this:</p>
  
  <pre><code>foreach (string name in Persons.Names)
  {
  // do something
  }
  </code></pre>
  
  <p>How would I do this?</p>
  
  <p>These are my classes:</p>
  
  <pre><code> class Person
      {
          public string Name
          {
              get;
              set;
          }
  
          public string Surname
          {
              get;
              set;
          }
      }
  
      class Persons : IEnumerable&lt;Person&gt;
      {
          List&lt;Person&gt; persons = new List&lt;Person&gt;();
          public IEnumerator&lt;Person&gt; GetEnumerator()
          {
              foreach (Person p in persons)
              {
                  yield return p;
              }
          }
  
          System.Collections.IEnumerator IEnumerable.GetEnumerator()
          {
              return GetEnumerator();
          }
  
          public IEnumerator&lt;string&gt; 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]; }
          }
      }
  </code></pre>