locked
IEnumerable interface RRS feed

  • Question

  • what is the IEnumerable interface, what does it provide or do...

     

     

    i am trying to understand iterators... and this is why i ask......

     

    Also

     

    public static  IEnumerable SimpleList();

     

    I am confused by this function signature what is public static....

     

    static means globally available i think .....

     

    what is public static.... why would you define like this....

     

    Tuesday, May 8, 2007 5:49 PM

Answers

  • Hi, lawwz

    Public is an access modifier, see: http://msdn2.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx

    So you'd take a look at this discussion: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1025888&SiteID=1

    Iterators allow you to easily create enumerable classes: classes that allow you to iterate through their collection of values via a foreach-in loop. Classes that implement the IEnumerable() interface allow you to use foreach-in against them. The IEnumerable interface demands that you implement a GetEmumerator method that returns an IEnumerator object. It's the IEnumerator object that allows a foreach-in to work the way it does, see: http://msdn2.microsoft.com/en-us/library/dscyy5s0(VS.80).aspx

    The Old Way of Creating Enumerable Classes: 

    Code Snippet
            public class DaysOfTheWeek : System.Collections.IEnumerable
    {
       
    string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

       
    public System.Collections.IEnumerator GetEnumerator()
        {
           
    for (int i = 0; i < m_Days.Length; i++)
            {
               
    return
    m_Days.GetEnumerator();
            }
        }
    }

     

    The New Way of Creating Enumerable Classes: 

    Code Snippet
            public class DaysOfTheWeek : System.Collections.IEnumerable
    {
       
    string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

       
    public System.Collections.IEnumerator GetEnumerator()
        {
           
    for (int i = 0; i < m_Days.Length; i++)
            {
                yield
    return
    m_Days[i];
            }
        }
    }

     

    Other helpful reference: http://www.codeproject.com/csharp/sssienumerable.asp

    So

     lawwz wrote:

    public static  IEnumerable SimpleList();

    is just a method which return an object which must be implemented IEnumerable interface.

     Thanks


    These links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Thursday, May 10, 2007 3:24 AM