不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C#自定义类实现集合类
编辑:dnawo 日期:2009-10-30
复制内容到剪贴板
程序代码

public class Person
{
private string _name;
private int _age;
public Person(string name, int age)
{
this._name = name;
this._age = age;
}
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public int Age
{
get { return this._age; }
set { this._age = value; }
}
}
{
private string _name;
private int _age;
public Person(string name, int age)
{
this._name = name;
this._age = age;
}
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public int Age
{
get { return this._age; }
set { this._age = value; }
}
}
上边为一个自定义类Person,下边是他的集合类PersonCollection:
复制内容到剪贴板
程序代码

public class PersonCollection : IEnumerable, IEnumerator
{
private Person[] _collection;
public PersonCollection(Person[] collection)
{
_collection = new Person[collection.Length];
for (int i = 0; i < collection.Length; i++)
_collection[i] = collection[i];
}
//IEnumerable实现部分
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator实现部分
private int position = -1;
public bool MoveNext()
{
position++;
return (position < _collection.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _collection[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
{
private Person[] _collection;
public PersonCollection(Person[] collection)
{
_collection = new Person[collection.Length];
for (int i = 0; i < collection.Length; i++)
_collection[i] = collection[i];
}
//IEnumerable实现部分
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator实现部分
private int position = -1;
public bool MoveNext()
{
position++;
return (position < _collection.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _collection[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
调用示例:
复制内容到剪贴板
程序代码

Person[] list = new Person[]{
new Person("张三",20),
new Person("李四",21),
new Person("王五",22)
};
PersonCollection collect = new PersonCollection(list);
foreach (Person person in collect)
MessageBox.Show(person.Name + "," + person.Age);
new Person("张三",20),
new Person("李四",21),
new Person("王五",22)
};
PersonCollection collect = new PersonCollection(list);
foreach (Person person in collect)
MessageBox.Show(person.Name + "," + person.Age);
评论: 0 | 引用: 0 | 查看次数: 7094
发表评论
请登录后再发表评论!