HOW TO:在 Visual C# .NET 中实现自定义集合:本分步指南介绍如何在 Visual C# .NET 中实现自定义集合。.NET 框架基类库提供了集合的正式定义:System.Collections.ICollection 接口。

吕的部落格唯女子与小人为难养也近之则不孙远之则怨

HOW TO:在 Visual C# .NET 中实现自定义集合

文章编号:307484
最后修改:2002年7月29日
修订:1.0
本文的发布号曾为 CHS307484

概要

本分步指南介绍如何在 Visual C# .NET 中实现自定义集合。.NET 框架基类库提供了集合的正式定义:System.Collections.ICollection 接口。

在自定义类中实现 ICollection 接口

ICollection 接口从 IEnumerable 接口继承而来。ICollection 接口定义了一个 CopyTo 方法和三个只读属性:IsSynchronizedSyncRoot CountICollection IEnumerable 接口继承 GetEnumerator 方法。自定义集合类应实现 ICollection 接口。

若要实现 ICollection 接口,请按照下列步骤操作:
1.在 Visual C# .NET 中,创建一个 Windows 应用程序。
2.在"解决方案资源管理器"中,右键单击该项目名称,指向添加,然后单击添加类来添加名为 CustomCollection 的类模块。
3.将以下代码示例添加到类模块的开头,以导入 System.Collection 名称空间:
using System.Collections;
4.用以下代码示例替代模块中的任何其他代码:
public class CustomCollection :ICollection
{

private int[] intArr = {1,5,9};
private int Ct;

public CustomCollection()
    {
	Ct=3;
    }
}
为了简便起见,CustomCollection 类包含一个有三个整数项和一个计数变量的数组。
5.实现 CopyTo 方法,该方法将一个整数数组和一个索引作为参数。此方法将一个集合中的各项复制到此数组中,以传入的索引为开始位置。若要实现此方法,请将以下代码粘贴到公共的 CustomCollection 构造函数之后:
void ICollection.CopyTo(Array myArr, int index)
{

foreach (int i in intArr)
    {
	myArr.SetValue(i,index);
	index = index+1;
    }

}
6.实现 GetEnumerator 方法,它是 ICollection 接口从 IEnumerable 中继承的。GetEnumerator 方法返回一个可迭代通过集合的 Enumerator 对象。将以下示例代码粘贴到 CopyTo 方法之后:
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(intArr);
}
7.若要实现这三个只读属性,请将以下代码粘贴到 GetEnumerator 方法后:
//The IsSynchronized Boolean property returns True if the
//collection is designed to be thread safe; otherwise, it returns False.
bool ICollection.IsSynchronized
{
get
    {
return false;
    }
}

//The SyncRoot property returns an object, which is used for synchronizing
//the collection.This returns the instance of the object or returns the
//SyncRoot of other collections if the collection contains other collections.
//
object ICollection.SyncRoot
{
get
    {
	return this;
    }
}

//The Count read-only property returns the number
//of items in the collection.
int ICollection.Count
{
get
    {
	return Ct;
    }
}

实现 GetEnumerator 方法的 Enumerator 对象

本节介绍如何创建可迭代通过 CustomCollection Enumerator 类。
1.将以下代码示例粘贴到类模块中的 End Class 语句之后:
public class Enumerator :IEnumerator
{
private int[] intArr;
private int Cursor;
}
声明 intArr 私有整数数组,使之在调用 GetEnumerator 方法时保存 CustomCollection 类的元素。Cursor 字段成员在枚举过程中保留当前的位置。
2.添加一个将 intArr 作为参数的构造函数,并将局部 intArr 设置为此 intArr。将以下代码示例粘贴到成员字段的声明后:
public Enumerator(int[] intarr)
{
this.intArr = intarr;
Cursor = -1;
}
3.实现 Reset MoveNext 方法。为此,请将以下代码粘贴到构造函数后:
void IEnumerator.Reset()
{
Cursor = -1;
}
bool IEnumerator.MoveNext()
{
if (Cursor < intArr.Length)
Cursor++;

return(!(Cursor == intArr.Length));
}
Reset 将光标设置到 -1,MoveNext 将光标移到下一元素。如果成功,则 MoveNext 返回 True。
4.实现 Current 只读属性,该属性返回光标所指的项。如果光标为 -1,则它生成一个 InvalidOperationException 类。将以下代码粘贴到 MoveNext 方法后面:
object IEnumerator.Current
{
get
    {
if((Cursor < 0) || (Cursor == intArr.Length))
	throw new InvalidOperationException();
	return intArr[Cursor];
    }
}

使用 For Each 迭代通过自定义集合

1.在 Form1.cs 中的设计选项卡上,向窗体拖动一个按钮。
2.双击该按钮,然后将以下示例代码添加到该按钮的 Click 事件中:
CustomCollection MyCol = new CustomCollection();

foreach (object MyObj in MyCol)
	MessageBox.Show(MyObj.ToString());
3.按 F5 键运行该应用程序,然后单击该按钮。请注意,此时出现一个消息框,显示自定义集合中的项。
工作原理是什么?For Each 调用 GetEnumerator 方法来创建 Enumerator 对象,并调用 MoveNext 方法将光标设置到第一项。然后,访问 Current 属性以获取 MyObj 中的项。重复这一步骤,直至 MoveNext 返回 False 为止。


这篇文章中的信息适用于:
Microsoft Visual C# .NET 2002 标准版
关键字: 
kbhowto kbhowtomaster KB307484
所属分类:杂七杂八标签:无 HOWTO:在VisualC#.NET实现自定义集合 吕 @ 2006年2月28日17点14分 PM 编辑 HOW TO:在 Visual C# .NET 中实现自定义集合阅读(4692) 评论(0)

相关文章

数据加载中,请稍候……