locked
IEnumerable<T> RRS feed

  • Question

  • When I code

    public class SomeInumerableDat :  IEnumerable<SqlDataRecord>

    {

    }

    And then click on Implement interface two methods are created.

     

    public class SomeInumerableDat :  IEnumerable<SqlDataRecord>

    {

    #region IEnumerable<SqlDataRecord> Members

     

    public IEnumerator<SqlDataRecord> GetEnumerator ()

    {

         throw new NotImplementedException ();

    }

     

    #endregion

     

    #region IEnumerable Members

     

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()

    {

         throw new NotImplementedException ();

    }

     

    #endregion

    }

     

    I am not sure what each one is used for. Do I put the same code in both? When does each get used?

     


    Tom
    Saturday, March 13, 2010 9:08 PM

Answers

  • Yes you can use the same code or common code. The idea is that IEnumerable is expressed in two different formats for two different consumers; hence it ensures a defacto overloading of those operations.

    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Tom Groszko Sunday, March 14, 2010 2:24 PM
    Saturday, March 13, 2010 9:29 PM
  • Specifically IEnumerable<T> derives from IEnumerable and so both interfaces must be implemented.

    public IEnumerator<SqlDataRecord> GetEnumerator ()

    {

         // create and return enumerator
    }

    IEnumerator IEnumerable.GetEnumerator()

    {

         return GetEnumerator(); // delegate to the one above
    }

    • Proposed as answer by paully21 Sunday, March 14, 2010 4:05 AM
    • Marked as answer by Tom Groszko Sunday, March 14, 2010 2:24 PM
    Saturday, March 13, 2010 10:00 PM

All replies

  • Yes you can use the same code or common code. The idea is that IEnumerable is expressed in two different formats for two different consumers; hence it ensures a defacto overloading of those operations.

    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by Tom Groszko Sunday, March 14, 2010 2:24 PM
    Saturday, March 13, 2010 9:29 PM
  • Specifically IEnumerable<T> derives from IEnumerable and so both interfaces must be implemented.

    public IEnumerator<SqlDataRecord> GetEnumerator ()

    {

         // create and return enumerator
    }

    IEnumerator IEnumerable.GetEnumerator()

    {

         return GetEnumerator(); // delegate to the one above
    }

    • Proposed as answer by paully21 Sunday, March 14, 2010 4:05 AM
    • Marked as answer by Tom Groszko Sunday, March 14, 2010 2:24 PM
    Saturday, March 13, 2010 10:00 PM