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

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>
              99视频精品| 亚洲午夜女主播在线直播| 久久久亚洲一区| 久久超碰97中文字幕| 国产专区综合网| 久久人人97超碰精品888| 久久国产日韩| 亚洲欧洲一区二区在线观看| 亚洲第一黄网| 欧美日韩亚洲激情| 亚洲欧美综合v| 久久国产加勒比精品无码| 国产综合亚洲精品一区二| 每日更新成人在线视频| 欧美精品成人在线| 午夜精品一区二区三区在线播放| 校园激情久久| 亚洲茄子视频| 亚洲一区二区久久| 在线播放一区| 亚洲欧洲日韩女同| 国产视频在线观看一区二区三区| 久久综合99re88久久爱| 欧美日本精品| 久久久久久久久综合| 欧美黄网免费在线观看| 午夜免费久久久久| 欧美成人蜜桃| 久久国产主播| 欧美日韩视频在线观看一区二区三区 | 欧美尤物巨大精品爽| 久久久蜜桃一区二区人| 中文在线资源观看视频网站免费不卡| 午夜精品国产精品大乳美女| 在线日韩欧美| 亚洲视频在线看| 亚洲国产你懂的| 亚洲女优在线| 中文网丁香综合网| 久久久久久久尹人综合网亚洲 | 国产日韩欧美a| 国产麻豆午夜三级精品| 久久视频在线看| 欧美视频一区二区三区…| 美女主播精品视频一二三四| 国产精品jvid在线观看蜜臀 | 亚洲一区二区高清| 久久久久国产一区二区三区| 性欧美18~19sex高清播放| 欧美高清视频在线观看| 久久在精品线影院精品国产| 国产精品美女久久久免费| 亚洲人成网站999久久久综合| 国产主播在线一区| 亚洲综合欧美日韩| 亚洲自拍另类| 欧美日本高清| 日韩视频免费观看| 一区二区欧美激情| 欧美精品三级| 亚洲免费av观看| 在线视频日本亚洲性| 欧美精品情趣视频| 亚洲欧洲精品一区二区三区| 亚洲精品欧美精品| 欧美国产日韩二区| 亚洲精品日韩一| 亚洲午夜在线| 国产精品男gay被猛男狂揉视频| 99ri日韩精品视频| 亚洲欧美日韩精品在线| 国产精品素人视频| 亚洲永久精品大片| 亚洲免费在线观看视频| 国产精品毛片一区二区三区| 一区二区三区成人| 亚洲国产欧美一区| 欧美va日韩va| 欧美激情一区二区三区四区 | 亚洲理论在线观看| 亚洲日本成人网| 欧美精品 国产精品| 亚洲三级免费| 亚洲欧美日韩高清| 国产一区二区日韩精品欧美精品| 欧美一级欧美一级在线播放| 久久女同互慰一区二区三区| 亚洲电影网站| 欧美日韩小视频| 欧美一区二区三区在线观看| 欧美1区视频| 亚洲一级黄色av| 激情成人av在线| 欧美高清在线播放| 亚洲精品视频免费观看| 欧美视频中文字幕| 欧美一区二区三区免费看| 欧美韩国日本综合| 亚洲欧美成人网| 永久555www成人免费| 欧美国产第一页| 亚洲欧美国产精品va在线观看| 葵司免费一区二区三区四区五区| 亚洲精品网站在线播放gif| 国产欧美日韩在线| 欧美成人一区在线| 亚洲欧美日韩精品久久奇米色影视| 久久躁日日躁aaaaxxxx| 亚洲少妇一区| 亚洲第一在线视频| 国产精品一级二级三级| 欧美肥婆在线| 久久aⅴ乱码一区二区三区| 亚洲伦理自拍| 欧美不卡在线视频| 欧美在线二区| 亚洲视频福利| 亚洲老司机av| 亚洲一区不卡| 欧美二区在线播放| 欧美亚洲一级片| 国产精品99久久久久久www| 亚洲丰满少妇videoshd| 国产日韩av一区二区| 欧美精品在线免费| 老司机久久99久久精品播放免费| 亚洲一区二区三区久久| 亚洲美女视频在线观看| 亚洲成在人线av| 快she精品国产999| 久久久久久亚洲精品中文字幕 | 亚洲成人自拍视频| 国产亚洲精品7777| 国产精品久久久久久久久久ktv| 欧美激情视频免费观看| 久久一区二区精品| 久久久久久久精| 羞羞答答国产精品www一本| 亚洲午夜高清视频| 亚洲一区二区三区色| 国产精品99久久不卡二区| 亚洲精品欧美日韩专区| 亚洲国产精品一区| 亚洲高清在线观看| 亚洲激情在线观看视频免费| 欧美激情国产精品| 亚洲激情在线激情| 亚洲国产精品美女| 亚洲精品一区二区三区婷婷月| 亚洲成色777777女色窝| 亚洲国产精品成人一区二区| 亚洲国产清纯| 日韩视频三区| 亚洲一区二区av电影| 亚洲一区精品视频| 欧美中文字幕在线视频| 久久久久99精品国产片| 裸体一区二区三区| 欧美精品成人一区二区在线观看| 欧美另类变人与禽xxxxx| 欧美午夜www高清视频| 国产精品视频一区二区三区 | 欧美精品一区三区| 欧美日韩国产精品一卡| 欧美三级免费| 国产欧美一区二区色老头| 激情综合中文娱乐网| 亚洲国产精品久久久久| 一区二区欧美在线| 性欧美18~19sex高清播放| 久久午夜精品一区二区| 欧美电影免费观看高清| 亚洲欧洲一区二区三区| 亚洲制服av| 久久综合久久88| 欧美亚男人的天堂| 精品不卡视频| 亚洲一区二区视频| 久久午夜国产精品| 亚洲精品无人区| 欧美在线资源| 欧美三区视频| 亚洲第一福利视频| 性欧美精品高清| 亚洲国产视频a| 性久久久久久久久| 欧美日韩福利视频| 尤物视频一区二区| 亚洲女爱视频在线| 国内伊人久久久久久网站视频| 亚洲人成久久| 久久久九九九九| 中文久久精品| 欧美国产丝袜视频| 国产日韩精品一区二区三区在线| 亚洲精选国产| 久久免费99精品久久久久久| 亚洲美女黄色| 欧美.日韩.国产.一区.二区| 国内精品久久久久伊人av|