青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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 夢(mèng)在天涯 閱讀(907) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816727
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              久久国产加勒比精品无码| 亚洲欧美日韩精品久久久久| 久久er精品视频| 欧美精品在线一区二区三区| 亚洲国产婷婷综合在线精品 | 黄页网站一区| 欧美一区二区三区在线看| 久久久久综合网| 久久精品国产视频| 久久婷婷麻豆| 久久久国产91| …久久精品99久久香蕉国产| 中文在线一区| 亚洲一区二区精品| 亚洲欧美激情四射在线日| 欧美精品在线观看91| 亚洲精品在线视频观看| 国产精品久久久久9999| 亚洲一区二区精品在线| 国产欧美一区二区在线观看| 欧美高清在线| 亚洲精品在线视频观看| 久久天天狠狠| 亚洲视频免费| 狂野欧美激情性xxxx欧美| 亚洲高清在线播放| 国产精品yjizz| 亚洲人体一区| 国产一区二区高清视频| 在线视频欧美日韩| 亚洲电影免费观看高清| 亚洲小说春色综合另类电影| 亚洲综合电影一区二区三区| 亚洲高清电影| 国产婷婷精品| 一区二区三区精品| 亚洲高清视频在线| 国产一级久久| 国产精品久久久一区麻豆最新章节 | 亚洲免费在线精品一区| 亚洲经典自拍| 久久综合伊人77777尤物| 在线亚洲观看| 亚洲一区二区在线视频| 亚洲人成免费| 亚洲精品视频在线看| 亚洲国产精品激情在线观看| 欧美一区二区三区啪啪| 欧美精品一级| 国产亚洲激情| 亚洲一区二区三区国产| 精品盗摄一区二区三区| 欧美精品一区二区三区久久久竹菊 | 在线免费观看视频一区| 欧美激情中文不卡| 久久综合国产精品| 一区二区高清在线观看| 激情五月综合色婷婷一区二区| 亚洲一区二区成人在线观看| 西西裸体人体做爰大胆久久久| 欧美亚洲专区| 亚洲欧美日韩国产一区二区三区| 国产主播一区二区三区| 欧美大胆a视频| 欧美中文字幕不卡| 亚洲久色影视| 久久久久久伊人| 欧美mv日韩mv亚洲| 久久一区二区精品| 亚洲欧美日韩在线| 亚洲日本一区二区三区| 巨胸喷奶水www久久久免费动漫| 日韩亚洲欧美在线观看| 狠狠入ady亚洲精品| 国产精品区二区三区日本| 欧美国产精品v| 久久精品99无色码中文字幕 | 1024精品一区二区三区| 国产精品99久久久久久有的能看| 亚洲欧美成人一区二区在线电影 | 欧美诱惑福利视频| 99精品热视频只有精品10| 亚洲精品免费在线观看| 久久婷婷影院| 久久伊人免费视频| 国产免费成人在线视频| 国产精品入口日韩视频大尺度| 久久一区亚洲| 麻豆精品一区二区综合av| 欧美成人综合| 欧美日韩一级黄| 亚洲靠逼com| 日韩一级片网址| 欧美视频在线看| 一本色道久久综合亚洲精品不| 影音先锋亚洲电影| 欧美在线亚洲综合一区| 久久精品亚洲热| 久久综合激情| 欧美人与性动交cc0o| 国产在线播放一区二区三区| 久久天天躁狠狠躁夜夜av| 久久激情久久| 狠狠色狠狠色综合日日91app| 欧美电影免费| 国产精品每日更新| 一区二区三区四区五区在线| 韩国三级电影久久久久久| av成人手机在线| 亚洲一区影院| 久久久久一区二区| 亚洲精品一区二区三区不| 欧美特黄一区| 久久精品视频在线观看| 好吊日精品视频| 麻豆成人综合网| 欧美日韩在线直播| 香蕉国产精品偷在线观看不卡 | 亚洲欧美自拍偷拍| 亚洲福利视频免费观看| 美国成人毛片| 一本色道**综合亚洲精品蜜桃冫| 国产欧美91| 久久国产精品久久久久久久久久 | 麻豆国产va免费精品高清在线| 欧美久久一区| 一区二区视频免费完整版观看| 亚洲黄色免费网站| 中日韩美女免费视频网址在线观看| 欧美亚洲一区二区三区| 久久亚洲欧美| 国产精品免费观看视频| 欧美成人一区二区三区在线观看| 欧美色偷偷大香| 久久一区中文字幕| 中文精品视频| 黄色亚洲在线| 中日韩美女免费视频网站在线观看| 在线免费观看视频一区| 亚洲综合精品一区二区| 亚洲国产精品久久久久| 久久精品一区二区三区四区| 亚洲精品美女在线观看| 国产午夜精品理论片a级探花| 亚洲精品久久久一区二区三区| 国产视频在线观看一区二区| 91久久精品国产91性色tv| 一级日韩一区在线观看| 亚洲视频一二| 亚洲小视频在线观看| 亚洲尤物在线视频观看| 香蕉久久一区二区不卡无毒影院| 亚洲免费伊人电影在线观看av| 亚洲欧美日韩成人高清在线一区| 亚洲视频精选| 香蕉乱码成人久久天堂爱免费 | 欧美午夜精品久久久久久人妖 | 欧美成人按摩| 欧美男人的天堂| 欧美日韩直播| 国产欧美在线| 亚洲激情视频网| 一本一本久久a久久精品牛牛影视| 亚洲图片在区色| 久久国产高清| 欧美激情精品久久久久久变态| 亚洲人成在线观看网站高清| 一区二区三区www| 午夜精品成人在线| 久久综合狠狠| 欧美午夜激情视频| 国产在线日韩| 日韩午夜在线电影| 欧美中文字幕视频在线观看| 美女精品在线观看| 日韩一区二区福利| 欧美在线日韩精品| 欧美另类变人与禽xxxxx| 国产精品视频自拍| 亚洲国产日韩在线一区模特| 亚洲一区二区三区视频| 久久人91精品久久久久久不卡| 91久久久久久久久| 亚洲欧美日韩在线| 欧美精品二区| 韩日午夜在线资源一区二区| 一区二区三区欧美成人| 另类综合日韩欧美亚洲| 日韩午夜av| 免费观看在线综合色| 国产精品欧美风情| 99国产精品久久| 久久一区免费| 亚洲综合导航| 欧美日韩亚洲综合一区| 亚洲国产婷婷香蕉久久久久久| 欧美影视一区| 亚洲精品综合久久中文字幕| 亚洲午夜久久久| 美女在线一区二区|