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

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 夢在天涯 閱讀(907) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1816728
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              久久精品视频免费播放| 一区二区精品| 欧美在线观看天堂一区二区三区| 亚洲高清中文字幕| 国产精一区二区三区| 欧美精品1区| 久久天天躁夜夜躁狠狠躁2022| 亚洲制服丝袜在线| 亚洲精品网站在线播放gif| 久久只精品国产| 久久成人免费日本黄色| 亚洲午夜在线观看| 亚洲免费观看高清完整版在线观看熊| 国产一区 二区 三区一级| 国产精品久久777777毛茸茸| 国产伦精品一区二区三区四区免费| 久久久噜噜噜久久狠狠50岁| 亚洲欧美另类久久久精品2019| 亚洲精品永久免费| 亚洲国产乱码最新视频| 麻豆成人在线观看| 久久久久久精| 久久都是精品| 欧美一区二区性| 午夜天堂精品久久久久 | 香蕉乱码成人久久天堂爱免费 | 亚洲香蕉网站| 99这里只有精品| 亚洲精品久久久一区二区三区| 亚洲大片免费看| 欧美激情成人在线| 欧美韩国日本综合| 亚洲电影专区| 亚洲国语精品自产拍在线观看| 亚洲电影下载| 亚洲国产高潮在线观看| 亚洲国产美女| 91久久精品国产91久久性色| 亚洲激情在线视频| 91久久国产综合久久91精品网站| 亚洲国产成人av| 亚洲精品在线观看免费| 99精品国产高清一区二区| 亚洲美女在线视频| 在线视频日韩| 午夜精品久久久久久久99樱桃 | 99pao成人国产永久免费视频| 亚洲三级免费| 亚洲私人影院| 欧美在线免费播放| 久久综合影音| 欧美精品亚洲精品| 国产精品久久久对白| 国产精品亚洲一区| 国内外成人在线视频| 亚洲风情在线资源站| 亚洲精品老司机| 亚洲一区久久久| 久久久久久久波多野高潮日日| 蜜桃av久久久亚洲精品| 亚洲欧洲偷拍精品| 亚洲一区二区免费看| 欧美在线电影| 欧美国产日韩二区| 国产精品久久久久久久一区探花 | 亚洲欧美日本另类| 久久久噜噜噜久久狠狠50岁| 欧美精品在线看| 国产精品区一区二区三区| 极品少妇一区二区三区| 亚洲每日在线| 久久精品91| 亚洲激情一区二区| 亚洲欧美另类在线| 美女网站久久| 国产精品日韩一区| 亚洲高清二区| 欧美一进一出视频| 亚洲国产国产亚洲一二三| 亚洲欧美日韩国产| 欧美黄色成人网| 国产日韩一区二区三区在线播放 | 久久久久久久网| 亚洲国产天堂网精品网站| 亚洲欧美激情视频| 欧美成人午夜激情视频| 国产亚洲精品一区二555| 亚洲精品小视频| 久久久久久久尹人综合网亚洲| 亚洲日本视频| 久久久免费av| 国产精品自在线| av成人免费在线观看| 另类亚洲自拍| 亚洲欧美一区二区三区在线 | 亚洲一区二区三区在线播放| 免费在线成人av| 亚洲一区视频在线| 欧美另类69精品久久久久9999| 国产综合视频在线观看| 亚洲欧美激情一区| 亚洲人成网站999久久久综合| 久久国产精品一区二区| 国产精品久久中文| 亚洲每日在线| 欧美国产高清| 久久精品久久99精品久久| 国产精品丝袜xxxxxxx| 一本色道久久88亚洲综合88| 六月婷婷一区| 欧美影院精品一区| 国产美女精品人人做人人爽| 亚洲社区在线观看| 亚洲日本成人女熟在线观看| 美女啪啪无遮挡免费久久网站| 国产综合网站| 欧美专区在线观看| 亚洲欧美国产高清| 国产精品免费区二区三区观看| 在线一区二区三区做爰视频网站| 亚洲国产成人午夜在线一区| 狼人天天伊人久久| 亚洲承认在线| 免费视频一区| 免费av成人在线| 亚洲国产清纯| 亚洲国产91| 欧美激情精品久久久久| 日韩视频在线观看国产| 亚洲人成网站精品片在线观看| 欧美成人免费在线| 日韩视频永久免费| 亚洲欧洲一区二区在线播放| 欧美激情视频免费观看| 99精品久久| 日韩亚洲欧美成人一区| 欧美天天在线| 亚洲欧美日韩在线综合| 亚洲欧美国产三级| 国产一区二区三区电影在线观看| 久久九九热免费视频| 久久久精品动漫| 亚洲国产va精品久久久不卡综合| 欧美国产精品| 欧美理论电影在线播放| 亚洲一级免费视频| 亚洲制服少妇| 精品不卡一区| 亚洲国产天堂久久国产91| 欧美日韩美女在线| 欧美亚洲一区在线| 久久精品国产清高在天天线| 亚洲电影在线观看| 亚洲欧洲一区二区三区久久| 欧美午夜激情小视频| 欧美一区二区视频在线| 久久婷婷久久| 一区二区三区视频在线看| 亚洲视频视频在线| 国模套图日韩精品一区二区| 欧美成人网在线| 欧美日韩免费看| 欧美中文在线字幕| 欧美91视频| 亚洲欧美国产精品专区久久| 欧美专区福利在线| 亚洲免费av片| 性色一区二区| 亚洲精选国产| 翔田千里一区二区| 亚洲日韩欧美一区二区在线| 亚洲私人影院在线观看| 精品粉嫩aⅴ一区二区三区四区| 最新国产精品拍自在线播放| 国产精品综合| 亚洲激情欧美激情| 国产情侣一区| 亚洲日本中文字幕免费在线不卡| 国产日本欧美一区二区| 亚洲国产片色| 国产在线高清精品| 亚洲毛片在线观看| 黄色一区三区| 一本一本久久| 亚洲国产精品尤物yw在线观看| 亚洲一区二区三区在线视频| 亚洲国产精品一区二区三区| 亚洲一区精品电影| 亚洲乱码国产乱码精品精| 欧美专区亚洲专区| 亚洲免费人成在线视频观看| 麻豆成人精品| 久久精品视频va| 国产精品激情| 亚洲欧洲一二三| 亚洲第一精品久久忘忧草社区| 亚洲资源在线观看| 一区二区毛片| 女人色偷偷aa久久天堂| 久久久久免费|