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

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>
              国产亚洲一区二区三区| 老色鬼久久亚洲一区二区| 亚洲图色在线| 久久av红桃一区二区小说| 久久久久国产精品人| 欧美成人伊人久久综合网| 欧美母乳在线| 国产精品老牛| 国内成人精品2018免费看| 亚洲国产成人精品久久| 亚洲午夜一区二区| 久久看片网站| 亚洲精品在线电影| 午夜伦欧美伦电影理论片| 乱码第一页成人| 欧美亚一区二区| 黄色一区二区三区| 一区二区三区欧美在线观看| 午夜日韩电影| 欧美大色视频| 亚洲女人天堂成人av在线| 久久美女性网| 国产精品久久久久久久app | 国产综合久久久久久鬼色| 在线精品一区| 亚洲欧美一区二区三区久久| 男女视频一区二区| 亚洲精品一区在线观看| 久久精选视频| 国产精品久久久久久久久婷婷 | 亚洲精品美女在线观看| 久久精品一区二区三区不卡| 亚洲美女区一区| 久久精品视频免费播放| 国产精品久久久久久超碰| 亚洲精品国产精品国产自| 久久久人成影片一区二区三区观看| 日韩视频一区二区| 女人色偷偷aa久久天堂| 国产中文一区二区| 午夜精品一区二区三区在线播放| 91久久午夜| 久久精品一区二区三区不卡| 国产精品欧美久久| 亚洲夜晚福利在线观看| 亚洲国产欧美日韩| 另类国产ts人妖高潮视频| 国产在线精品二区| 久久精品1区| 性欧美1819sex性高清| 国产精品区一区二区三| 亚洲免费一级电影| 亚洲婷婷在线| 国产精品久久福利| 欧美一级久久久| 午夜在线视频一区二区区别 | 一区二区三区视频在线观看 | 久久久久久999| 国产有码在线一区二区视频| 欧美影片第一页| 午夜精品亚洲| 国产一区二三区| 久久影视精品| 久久精品一本久久99精品| 精品9999| 亚洲国产成人精品久久| 欧美精品自拍| 亚洲综合日韩| 午夜精品亚洲一区二区三区嫩草| 国产精品一区二区在线| 久久久久国产精品一区| 久久综合给合| 一区二区日韩| 亚洲欧美大片| 在线成人中文字幕| 亚洲黄网站黄| 国产精品久久一级| 久久婷婷综合激情| 欧美精品日韩www.p站| 99在线精品免费视频九九视| 亚洲精品一级| 国产亚洲第一区| 欧美成人有码| 国产精品毛片va一区二区三区| 欧美在线免费| 美女图片一区二区| 亚洲一区视频| 久久精品官网| 一本色道**综合亚洲精品蜜桃冫 | 午夜精品福利一区二区蜜股av| 国产一区视频在线观看免费| 欧美激情第一页xxx| 国产精品久久久久久久第一福利 | 久久中文字幕一区| 欧美日韩二区三区| 久久免费国产精品| 欧美精品日本| 久久久久久夜| 欧美色综合天天久久综合精品| 久久久久**毛片大全| 欧美日韩成人在线视频| 久久久夜精品| 国产精品久久影院| 亚洲国产精品久久久久| 国产日韩一区二区三区在线| 欧美国产在线观看| 国产一区二区三区的电影| 日韩午夜免费| 最新国产成人在线观看| 欧美一级精品大片| 亚洲欧美日韩在线| 欧美精品国产一区| 欧美α欧美αv大片| 国产日韩欧美一区在线| 一本色道久久综合亚洲精品婷婷 | 亚洲激情黄色| 久久蜜桃香蕉精品一区二区三区| 亚洲欧美国产视频| 欧美日韩大陆在线| 亚洲国产精品精华液网站| 在线精品视频一区二区| 欧美一区影院| 欧美综合国产精品久久丁香| 国产精品国产一区二区| 99国产精品国产精品久久| 亚洲最新色图| 欧美乱在线观看| 亚洲国产专区校园欧美| 亚洲人线精品午夜| 欧美成人r级一区二区三区| 牛牛影视久久网| 亚洲二区在线观看| 久久综合久久综合久久综合| 蜜臀a∨国产成人精品| 韩国福利一区| 久久久久久久欧美精品| 玖玖玖国产精品| 精品成人在线观看| 久久久久国产免费免费| 欧美成人在线免费观看| 亚洲人精品午夜| 欧美精品麻豆| 中日韩高清电影网| 午夜视频在线观看一区二区三区| 国产精品免费看| 欧美在线黄色| 欧美成人一区二区| 日韩午夜电影av| 国产精品啊啊啊| 亚洲欧美国产日韩天堂区| 久久国产精品一区二区三区四区| 国产在线拍偷自揄拍精品| 久久漫画官网| 亚洲国产精品女人久久久| 日韩一区二区免费高清| 欧美午夜片在线观看| 欧美一区二区三区日韩| 欧美成人精品一区二区三区| 亚洲美女精品成人在线视频| 国产精品yjizz| 久久精品99无色码中文字幕| 亚洲电影免费观看高清完整版在线观看 | 亚洲一区999| 国产亚洲一区二区三区在线播放| 久久人人爽爽爽人久久久| 亚洲精品视频免费在线观看| 午夜在线成人av| 91久久国产综合久久91精品网站| 欧美视频中文在线看 | 亚洲最快最全在线视频| 久久精品国产2020观看福利| 亚洲人成久久| 国产私拍一区| 欧美国产日韩免费| 欧美一区二区三区四区视频| 91久久精品美女高潮| 久久久久久网址| 亚洲午夜在线观看| 在线日韩一区二区| 国产精品一区二区久久精品| 免费在线亚洲欧美| 亚洲欧美日韩天堂| 亚洲精品一区在线观看香蕉| 久久久亚洲精品一区二区三区| 在线亚洲精品| 亚洲成色777777女色窝| 国产精品视频久久| 欧美日产在线观看| 老牛嫩草一区二区三区日本| 午夜在线视频一区二区区别| 99视频在线精品国自产拍免费观看| 免费久久99精品国产| 欧美尤物巨大精品爽| 在线视频日本亚洲性| 亚洲欧洲另类| 在线看日韩欧美| 黄色成人在线| 国产日韩欧美亚洲一区| 国产精品日本欧美一区二区三区| 欧美日韩国产天堂|