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

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

搜索

  •  

積分與排名

  • 積分 - 1811728
  • 排名 - 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>
              亚洲少妇中出一区| 一区二区亚洲精品国产| 一本色道综合亚洲| 日韩亚洲欧美成人| 国产精品福利在线观看| 午夜在线一区二区| 欧美一区二区视频在线| 伊人久久大香线| 亚洲国产成人久久| 欧美日韩美女在线观看| 性欧美暴力猛交69hd| 久久久精品一区| 99国内精品| 性久久久久久久久久久久| 在线成人性视频| 99re视频这里只有精品| 国产精品亚洲а∨天堂免在线| 久久九九国产精品| 女同性一区二区三区人了人一| 亚洲网站在线观看| 欧美一级久久久| 欧美日韩综合在线| 西瓜成人精品人成网站| 美女精品在线观看| 午夜精品免费视频| 蜜桃av噜噜一区二区三区| 亚洲午夜一二三区视频| 久久av红桃一区二区小说| 亚洲精品在线三区| 91久久国产精品91久久性色| 亚洲人成人99网站| 国产欧美一区二区精品秋霞影院| 亚洲大片精品永久免费| 国产精品视频网址| 亚洲人成欧美中文字幕| 一色屋精品视频在线观看网站| 一本久道久久综合婷婷鲸鱼| 1024国产精品| 亚洲男人第一av网站| 亚洲乱码精品一二三四区日韩在线| 欧美亚洲综合另类| 亚洲小说区图片区| 欧美国产视频日韩| 欧美成人激情视频| 狠狠色狠狠色综合人人| 亚洲欧美日本在线| 亚洲尤物在线视频观看| 欧美裸体一区二区三区| 欧美刺激性大交免费视频| 国产日韩精品入口| 亚洲欧美经典视频| 亚洲欧美日本日韩| 欧美特黄一级大片| 日韩视频在线观看免费| 亚洲另类在线视频| 欧美成人午夜激情视频| 免费人成精品欧美精品| 精品二区视频| 久久久www成人免费精品| 欧美一区二区免费| 国产精品久久久久久妇女6080 | 亚洲在线观看视频网站| 欧美激情视频网站| 亚洲日本va在线观看| 99ri日韩精品视频| 欧美精品一区二区三区四区| 亚洲国产精品v| 99精品国产福利在线观看免费| 欧美mv日韩mv国产网站app| 欧美激情a∨在线视频播放| 在线精品视频一区二区| 狼狼综合久久久久综合网| 欧美成人一区二区三区在线观看| 亚洲国产成人久久| 欧美99久久| 亚洲精品极品| 午夜精品国产更新| 国产拍揄自揄精品视频麻豆| 久久精品国产亚洲精品| 欧美freesex交免费视频| 亚洲国产日韩欧美综合久久| 欧美日本韩国一区二区三区| 一本色道综合亚洲| 久久国产精彩视频| 亚洲第一区在线| 欧美激情一区二区| 宅男在线国产精品| 老司机精品视频网站| 亚洲日韩欧美视频一区| 国产精品v欧美精品v日韩精品| 午夜精品区一区二区三| 亚洲成色精品| 欧美一区2区视频在线观看| 激情久久五月天| 欧美人与性动交α欧美精品济南到| 亚洲视频一区二区在线观看| 久久中文字幕导航| 久久久亚洲人| 亚洲欧洲一区| 久久久国产精品一区二区三区| 亚洲欧洲视频| 国产日产欧产精品推荐色 | 亚洲天堂视频在线观看| 麻豆精品视频在线观看视频| 一区二区三区成人精品| 国产偷国产偷精品高清尤物| 欧美成人视屏| 欧美中文字幕视频| 99re66热这里只有精品3直播| 久久久久中文| 亚洲午夜视频在线观看| 在线观看亚洲精品视频| 国产精品免费网站在线观看| 欧美成人国产| 久久免费高清视频| 亚洲永久精品国产| 亚洲美女色禁图| 欧美成人精品在线视频| 久久se精品一区二区| 亚洲视频网站在线观看| 亚洲国产精品一区二区第一页 | 久久一区亚洲| 亚洲欧美日韩一区二区| 亚洲精品少妇30p| 欧美成人免费观看| 久久综合色播五月| 久久国产一区二区| 午夜国产精品影院在线观看| 中文国产一区| 一区二区久久| 日韩天堂在线观看| 亚洲国产精品国自产拍av秋霞 | 国产精品v日韩精品v欧美精品网站| 免费看成人av| 欧美3dxxxxhd| 欧美xart系列在线观看| 久久综合狠狠综合久久激情| 久久久国产成人精品| 午夜综合激情| 欧美中文字幕在线播放| 午夜精品久久久久久久99水蜜桃 | 欧美日韩一区二区在线视频 | 亚洲一级片在线看| 亚洲社区在线观看| 亚洲一卡久久| 性刺激综合网| 久久精品国产亚洲a| 久久久久久亚洲综合影院红桃| 久久久精品tv| 鲁鲁狠狠狠7777一区二区| 免费亚洲一区| 欧美日韩二区三区| 国产精品久久久久一区二区| 国产美女精品在线| 国产色爱av资源综合区| 狠狠干综合网| 亚洲免费久久| 亚洲综合欧美日韩| 久久国产手机看片| 嫩草国产精品入口| 亚洲欧洲精品一区二区| 99国产精品视频免费观看| 亚洲少妇最新在线视频| 欧美在线关看| 欧美激情日韩| 国产精品爽黄69| 玉米视频成人免费看| 日韩视频免费在线| 欧美在线观看一二区| 免费久久99精品国产自在现线| 亚洲国产mv| 亚洲一区欧美| 久久综合一区二区| 国产精品jizz在线观看美国| 激情综合色综合久久综合| 亚洲精品欧洲精品| 欧美在线国产精品| 亚洲电影免费在线 | 久久久久久电影| 亚洲精品久久久久久一区二区| 亚洲欧美日韩在线| 欧美国产日韩一区二区三区| 国产欧美另类| 99精品热视频| 男同欧美伦乱| 亚洲一区二区三区视频| 免费成人毛片| 国产手机视频精品| 99亚洲伊人久久精品影院红桃| 久久久天天操| 日韩视频欧美视频| 久久久夜夜夜| 国产视频观看一区| 亚洲午夜一区二区| 亚洲福利国产精品| 久久久久久免费| 国产婷婷一区二区| 亚洲综合视频网| 亚洲免费观看在线观看| 免费一级欧美片在线播放|