• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Implementing IEnumerator and IEnumerable Interfaces using Inner Class in C#(very good)

            .NET framework provides IEnumerable and IEnumerator interfaces to implement collection
            like behavior to user defined classes. A
            developer can implement these interfaces to?
            provide collection like behavior to their classes. These interfaces are implemented?
            through inner classes.

            An inner class ( also known as Nested Type ) is a class which is enclosed inside
            another class.

            class A
            {
            int i ;
            class B // Class B is a inner class or Nested Type?
            {
            }
            }?

            IEnumerator and IEnumerable interfaces are defined in System.Collections namespace as :

            public interface IEnumerable
            {
            IEnumerator GetEnumerator(); //Returns a Enumerator
            }

            public interface IEnumerator
            {
            bool MoveNext(); //An enumerator is always positioned before the?
            //first element of the collection, first call to MoveNext?
            //moves the enumerator over the first element of the
            //collection?

            object Current { get ; } //Returns current object from the collection

            void Reset(); //Resets enumerator to just above the first element of the collection.
            }?

            For more details about above interfaces, refer
            Microsoft .NET framework documentation.

            Above two interfaces must be implemented to provide a collection (similar to ArrayList ) like
            behavior to user defined classes. There is one more interface called Collection interface?
            which I am excluding from our discussion.

            To demonstrate the working of above interfaces and inner class I have created a class called
            ItemCollection which implements IEnumerable interface.

            Within ItemCollection class there is one more class ( inner class or Nested Type ) called
            ItemIterator which implements IEnumerator interface. ItemCollection class also contains a?
            string array itemId, which provides the basis for iteration.?

            For implementation details, see the code below and follow these steps :

            1. Create a file ItemCollection.cs and save the source in that.

            //File ItemCollection.cs
            using System;
            using System.Collections;?

            //Class ItemCollection implements IEnumerable interface
            class ItemCollection : IEnumerable
            {
            String[] itemId ;

            //Constructor to create and populate itemId String array
            public ItemCollection( int noOfItem )
            {
            itemId = new String[noOfItem] ;
            for(int i = 0; i < itemId.Length; i ++ )
            {
            itemId[i] = i.ToString();
            }
            }

            //Implementation of method GetEnumerator of IEnumerable interface
            public virtual IEnumerator GetEnumerator()
            {
            return new ItemIterator(this);
            }


            //Inner class ItemIterator, implements IEnumerator
            public class ItemIterator : IEnumerator
            {
            //Declare a variable of type ItemCollection,
            //to keep reference to enclosing class instance
            private ItemCollection itemCollection;

            //Declare a integer pointer and Set to -1, so that
            //first call to MoveNext moves the enumerator over?
            //the first element of the collection.
            private int index = -1 ;


            //Pass an instance of enclosing class
            public ItemIterator(ItemCollection ic)
            {
            //Save enclosing class reference?
            itemCollection = ic ;
            }

            //After an enumerator is created or after a Reset,
            //an enumerator is positioned before the first element
            //of the collection, and the first call to MoveNext?
            //moves the enumerator over the first element of the
            //collection.

            public bool MoveNext()
            {
            index++ ;
            if( index < itemCollection.itemId.Length )
            {
            return true ;
            }
            else
            {
            index = -1;
            return false;
            }
            }

            //Return the current object, in our case Item Id string?
            //from itemId[] array. Throws InvalidOperationException exception
            //if index pointing to wrong position
            public object Current
            {
            get
            {
            if( index <= -1 )?
            {
            throw new InvalidOperationException() ;
            }
            return itemCollection.itemId[index];
            }
            }

            //Reset pointer to -1
            public void Reset()
            {
            index = -1;
            }

            }

            public static int Main(String[] args)
            {
            //Instantiate the collection
            ItemCollection itemCol = new ItemCollection(10);

            //Iterate the collection with various looping construct
            //provided in c#
            Console.WriteLine("1. Iteration using foreach loop:");
            foreach( String itemIdStr in itemCol)
            {
            Console.Write(itemIdStr + " " );
            }

            Console.WriteLine("\n\n2. Iteration using for loop:");
            for(IEnumerator ie = itemCol.GetEnumerator() ;
            ie.MoveNext();)
            {
            Console.Write(ie.Current + " " );
            }

            Console.WriteLine("\n\n3. Iteration using while loop:");
            IEnumerator ie1 = itemCol.GetEnumerator();
            while(ie1.MoveNext())
            {
            Console.Write(ie1.Current + " ");
            }
            return 0;
            }

            }


            2. Compile the above code as

            csc ItemCollection.cs

            3. Run the file ItemCollection.exe to see following output in console window :

            1. Iteration with foreach loop:
            0 1 2 3 4 5 6 7 8 9

            2. Iteration with for loop:
            0 1 2 3 4 5 6 7 8 9

            3. Iteration with while loop:
            0 1 2 3 4 5 6 7 8 9



            posted on 2006-04-11 12:04 夢在天涯 閱讀(887) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            精品久久久久中文字幕一区| 国产精品女同久久久久电影院 | 亚洲精品美女久久久久99小说 | 久久99精品久久只有精品| 国产一区二区三区久久精品| 九九热久久免费视频| 狠狠综合久久AV一区二区三区| 久久精品无码午夜福利理论片| 狠狠精品干练久久久无码中文字幕 | 99久久精品免费国产大片| 久久久国产视频| 国产精品va久久久久久久| 日本久久久久亚洲中字幕| 久久丝袜精品中文字幕| 国产一区二区三区久久| 亚洲精品国产字幕久久不卡| 久久e热在这里只有国产中文精品99 | 久久国产成人| 久久福利青草精品资源站| 久久天天躁夜夜躁狠狠躁2022| 久久免费高清视频| jizzjizz国产精品久久| 国内精品人妻无码久久久影院导航 | 岛国搬运www久久| 日韩人妻无码精品久久久不卡 | 99精品国产在热久久| 亚洲国产另类久久久精品小说| 久久精品国产黑森林| 久久精品不卡| 少妇久久久久久被弄到高潮 | 亚洲伊人久久大香线蕉综合图片| 青青热久久国产久精品| 久久久无码精品亚洲日韩软件| 久久精品国产亚洲一区二区| 狠狠色丁香久久综合五月| 国产精品无码久久综合| 久久91综合国产91久久精品| 久久噜噜电影你懂的| 久久久精品视频免费观看| 久久www免费人成看国产片| 久久久精品视频免费观看|