C#复习笔记(7)
2006-12-18 00:07
381 查看
接口
using System;
using System.Collections;
// Declare the collection and implement the IEnumerable interface:
public class MyCollection: IEnumerable
{
int[] items;
public MyCollection()
{
items = new int[5] {12, 44, 33, 2, 50};
}
public MyEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
// Implement the GetEnumerator() method:
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
//以上两个接口实现方法可以用下面的一个方法代替
//
// public IEnumerator GetEnumerator()
// {
// return new MyEnumerator(this);
// }
// Declare the enumerator and implement the IEnumerator
// and IDisposable interfaces
public class MyEnumerator: IEnumerator, IDisposable
{
int nIndex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nIndex = -1;
}
public void Reset()
{
nIndex = -1;
}
public bool MoveNext()
{
nIndex++;
return (nIndex < collection.items.GetLength(0));
}
public int Current
{
get
{
return (collection.items[nIndex]);
}
}
// The current property on the IEnumerator interface:
object IEnumerator.Current
{
get
{
return (Current);
}
}
public void Dispose()
{
Console.WriteLine("In Dispose");
collection = null;
}
}
}
public class MainClass
{
public static void Main(string [] args)
{
MyCollection col = new MyCollection();
Console.WriteLine("Values in the collection are:");
// Display collection items:
foreach (int i in col)
{
Console.WriteLine(i);
}
}
}
using System;
using System.Collections;
// Declare the collection and implement the IEnumerable interface:
public class MyCollection: IEnumerable
{
int[] items;
public MyCollection()
{
items = new int[5] {12, 44, 33, 2, 50};
}
public MyEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
// Implement the GetEnumerator() method:
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
//以上两个接口实现方法可以用下面的一个方法代替
//
// public IEnumerator GetEnumerator()
// {
// return new MyEnumerator(this);
// }
// Declare the enumerator and implement the IEnumerator
// and IDisposable interfaces
public class MyEnumerator: IEnumerator, IDisposable
{
int nIndex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nIndex = -1;
}
public void Reset()
{
nIndex = -1;
}
public bool MoveNext()
{
nIndex++;
return (nIndex < collection.items.GetLength(0));
}
public int Current
{
get
{
return (collection.items[nIndex]);
}
}
// The current property on the IEnumerator interface:
object IEnumerator.Current
{
get
{
return (Current);
}
}
public void Dispose()
{
Console.WriteLine("In Dispose");
collection = null;
}
}
}
public class MainClass
{
public static void Main(string [] args)
{
MyCollection col = new MyCollection();
Console.WriteLine("Values in the collection are:");
// Display collection items:
foreach (int i in col)
{
Console.WriteLine(i);
}
}
}
相关文章推荐
- 复习笔记二 二叉排序树(C++ C#)
- .Net学习笔记----2015-07-23(C#基础复习11,字符串复习)
- .Net学习笔记----2015-07-15(C#基础复习01)
- C# 复习笔记 消息与事件处理
- C#复习笔记(3)
- (1-2)章深入.NET平台和C#编程 复习的时候 整理的笔记
- .Net学习笔记----2015-07-21(C#基础复习08,模拟移动硬盘U盘等)
- .Net学习笔记----2015-07-22(C#基础复习10,面向对象2个小练习)
- C# 多线程复习笔记
- .Net学习笔记----2015-07-21(C#基础复习03,简单工厂和抽象类)
- C#复习笔记(4)
- C#学习笔记----复习笔记
- C#复习笔记(8)
- .Net学习笔记----2015-07-22(C#基础复习09,虚方法、抽象方法、接口复习)
- .Net学习笔记----2015-07-16(C#基础复习02)
- .Net学习笔记----2015-07-21(C#基础复习04,面向对象语法和继承复习)
- c#复习笔记1
- .Net学习笔记----2015-07-21(C#基础复习07,关键字、访问修饰符)
- .Net学习笔记----2015-07-21(C#基础复习05,里氏转换)
- C#复习笔记