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

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>
              欧美精品一区二区三区蜜臀| 欧美色大人视频| 亚洲高清在线播放| 免费亚洲婷婷| 欧美韩日一区二区| 一本色道久久综合| 亚洲一本视频| 黄色成人片子| 亚洲第一区色| 欧美日韩一区二区国产| 亚洲字幕在线观看| 久久精品99久久香蕉国产色戒| 红桃av永久久久| 亚洲欧洲综合另类| 国产精品劲爆视频| 久久综合九色综合欧美狠狠| 欧美成人蜜桃| 欧美伊人久久久久久久久影院| 久久久欧美精品| 亚洲天堂成人| 久久精品国产精品| 一区二区欧美日韩| 久久久久免费| 亚洲专区在线视频| 美日韩精品视频| 午夜在线精品| 欧美日本高清一区| 久久免费视频网| 欧美私人啪啪vps| 欧美福利视频网站| 国产噜噜噜噜噜久久久久久久久| 欧美国产高清| 国产主播精品在线| 99视频日韩| 亚洲精品国产日韩| 久久精品国产69国产精品亚洲 | 一区电影在线观看| 影音先锋成人资源站| 一区二区三区福利| 亚洲人成在线免费观看| 午夜一区不卡| 亚洲午夜国产一区99re久久| 久久视频在线看| 久久国产精品99精品国产| 欧美日韩爆操| 亚洲国产经典视频| 在线精品国产成人综合| 欧美一区二区三区喷汁尤物| 亚洲午夜一区二区三区| 免费成人黄色片| 欧美成人国产va精品日本一级| 国产欧美日韩视频| 亚洲一区欧美激情| 亚洲天堂久久| 欧美日韩在线亚洲一区蜜芽 | 小嫩嫩精品导航| 欧美日韩一区二区国产| 最近中文字幕日韩精品| 亚洲韩国精品一区| 免费不卡亚洲欧美| 玖玖玖国产精品| 韩日欧美一区二区三区| 久久黄色影院| 欧美a级理论片| 亚洲第一精品福利| 久久亚洲欧美| 亚洲高清影视| 99精品福利视频| 欧美日韩亚洲视频一区| 一区二区三区久久精品| 亚洲一区中文| 国产情侣久久| 久久久精品一区| 男人的天堂成人在线| 亚洲黄色精品| 欧美精品18videos性欧美| 亚洲精品一区中文| 亚洲伊人久久综合| 国产日韩一区在线| 久久久欧美一区二区| 欧美激情在线免费观看| 这里只有精品视频| 国产精品一区二区在线观看不卡| 亚洲欧美影院| 免费在线亚洲欧美| 99这里有精品| 国产欧美日韩视频一区二区三区| 欧美在线观看天堂一区二区三区| 久热国产精品视频| 一区二区三区产品免费精品久久75 | 蜜桃av噜噜一区| 亚洲美女中出| 国产精品永久入口久久久| 久久电影一区| 亚洲精品1区2区| 欧美伊久线香蕉线新在线| **欧美日韩vr在线| 欧美午夜电影一区| 久久久午夜电影| 亚洲毛片av在线| 久久漫画官网| 亚洲一区二区久久| 国内外成人免费激情在线视频网站| 免费成人在线视频网站| 亚洲午夜国产一区99re久久| 老司机aⅴ在线精品导航| 宅男噜噜噜66国产日韩在线观看| 国产精品一区二区欧美| 欧美国产精品一区| 欧美一区激情视频在线观看| 亚洲精品国精品久久99热| 久久精品观看| 亚洲欧美日韩国产一区| 亚洲精品视频在线| 韩国视频理论视频久久| 国产精品极品美女粉嫩高清在线| 久久久一区二区| 亚洲欧美日韩在线不卡| 亚洲美女毛片| 免费欧美在线| 久久米奇亚洲| 亚洲夜晚福利在线观看| 亚洲精品色婷婷福利天堂| 狠狠久久婷婷| 国产一区二区三区成人欧美日韩在线观看| 欧美久久久久久久久| 另类专区欧美制服同性| 久久久av毛片精品| 亚洲综合首页| 亚洲一区日韩在线| 夜夜嗨一区二区| 91久久嫩草影院一区二区| 蜜臀久久99精品久久久久久9| 欧美一区永久视频免费观看| 亚洲私人影院在线观看| av不卡在线看| 亚洲卡通欧美制服中文| 亚洲人成网站精品片在线观看| 揄拍成人国产精品视频| 国产一区二区三区久久久久久久久| 国产精品成人一区二区网站软件| 欧美精品久久久久久久免费观看| 免费91麻豆精品国产自产在线观看| 久久久久久久久一区二区| 久久久精品性| 久久在线观看视频| 猛干欧美女孩| 欧美激情精品久久久久久蜜臀 | 日韩视频在线永久播放| 亚洲美洲欧洲综合国产一区| 亚洲人午夜精品免费| 亚洲精品在线免费观看视频| 亚洲精品亚洲人成人网| 一区二区精品在线| 一本色道久久综合狠狠躁篇的优点 | 亚洲国内精品| 亚洲精品综合精品自拍| 一区二区三区国产| 亚洲欧美三级伦理| 久久激情婷婷| 欧美99在线视频观看| 欧美日韩高清免费| 国产美女一区二区| 在线不卡免费欧美| 一本色道久久加勒比精品| 亚洲女同在线| 久久久人成影片一区二区三区观看| 久久综合一区二区| 亚洲人成毛片在线播放女女| 在线一区欧美| 久久成人精品| 欧美美女福利视频| 国产亚洲欧美另类一区二区三区| 激情欧美一区二区三区| 99re热这里只有精品视频| 午夜性色一区二区三区免费视频| 久久九九精品99国产精品| 亚洲国产精品美女| 亚洲欧美日韩中文视频| 美女视频黄a大片欧美| 国产精品电影在线观看| 亚洲大胆美女视频| 午夜精品国产| 欧美激情一区二区三区在线视频观看 | 亚洲最新在线视频| 久久免费黄色| 99视频日韩| 免费日韩av电影| 国产精品免费一区二区三区在线观看| 激情综合自拍| 香蕉免费一区二区三区在线观看| 欧美大片va欧美在线播放| 在线一区二区三区做爰视频网站 | 中文精品一区二区三区| 久久网站热最新地址| 国产精品久久久免费| 日韩午夜免费视频| 久久婷婷久久| 亚洲欧美日韩国产综合精品二区| 欧美激情精品久久久久久大尺度|