实现接口 IEnumerator, IEnumerable
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace KenDemoApp
{
/*
//class UserCollection : IEnumerable
class UserCollection
{
public UserCollection()
{
userList.Add("Ken");
userList.Add("Tommy");
}
ArrayList userList = new ArrayList();
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new MyEnumrator(userList);
}
#endregion
}
*/
class UserCollection
{
public UserCollection()
{
userList.Add("Ken");
userList.Add("Tommy");
}
ArrayList userList = new ArrayList();
//public IEnumerable List
//{
// get
// {
// return (IEnumerable)userList;
// }
//}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return userList.GetEnumerator();
}
#endregion
}
//class MyEnumrator: IEnumerator
class MyEnumrator
{
ArrayList list = new ArrayList();
int index;
public MyEnumrator(ArrayList l)
{
Reset();
list = l;
}
#region IEnumerator Members
public object Current
{
get {
if (index < list.Count)
return list[index];
else
return null;
}
}
public bool MoveNext()
{
index++;
return index < list.Count;
}
public void Reset()
{
index = -1;
}
#endregion
}
//mutator interface!!!!!!!!!!!!!!!!!!!!!!!!!!!作用和好处:1.访问集合中的值类型数据 2.不需要拆箱
interface IUser
{
string Name { get;set;}
}
//class User
struct User: IUser
{
public User(string name)
{
this.name = name;
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class UserManager
{
public ArrayList onlineUsers = new ArrayList();
public UserManager()
{
for (int i = 0; i < 10000000; i++)
onlineUsers.Add(new User(i.ToString()));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace KenDemoApp
{
/*
//class UserCollection : IEnumerable
class UserCollection
{
public UserCollection()
{
userList.Add("Ken");
userList.Add("Tommy");
}
ArrayList userList = new ArrayList();
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return new MyEnumrator(userList);
}
#endregion
}
*/
class UserCollection
{
public UserCollection()
{
userList.Add("Ken");
userList.Add("Tommy");
}
ArrayList userList = new ArrayList();
//public IEnumerable List
//{
// get
// {
// return (IEnumerable)userList;
// }
//}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return userList.GetEnumerator();
}
#endregion
}
//class MyEnumrator: IEnumerator
class MyEnumrator
{
ArrayList list = new ArrayList();
int index;
public MyEnumrator(ArrayList l)
{
Reset();
list = l;
}
#region IEnumerator Members
public object Current
{
get {
if (index < list.Count)
return list[index];
else
return null;
}
}
public bool MoveNext()
{
index++;
return index < list.Count;
}
public void Reset()
{
index = -1;
}
#endregion
}
//mutator interface!!!!!!!!!!!!!!!!!!!!!!!!!!!作用和好处:1.访问集合中的值类型数据 2.不需要拆箱
interface IUser
{
string Name { get;set;}
}
//class User
struct User: IUser
{
public User(string name)
{
this.name = name;
}
string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class UserManager
{
public ArrayList onlineUsers = new ArrayList();
public UserManager()
{
for (int i = 0; i < 10000000; i++)
onlineUsers.Add(new User(i.ToString()));
}
}
}
return userList.GetEnumerator(); 这里,可以改为迭代器直接实现(C#2.0支持):
for (int i = 0; i < userList.Count; i++)
yield return userList[i];
yield return userList[i];
调用代码,实例:
Code
//UserCollection users = new UserCollection();
//((ArrayList)users.GetEnumerator()).RemoveAt(0);
//IEnumerator iterator = users.GetEnumerator();
//while (iterator.MoveNext())
// Console.WriteLine(iterator.Current);
//foreach (object obj in users)
// Console.WriteLine(obj);
//MyEnumrator myEnum = new MyEnumrator(new ArrayList() { "AAa", "bCCC" });
//如果实现了MoveNext方法,Current属性等,就可以通过下面的方法遍历对象集合;实际上,此时,刚好正是实现了接口IEnumerator。
//while (myEnum.MoveNext())
// Console.WriteLine(myEnum.Current);
//UserCollection users = new UserCollection();
//((ArrayList)users.GetEnumerator()).RemoveAt(0);
//IEnumerator iterator = users.GetEnumerator();
//while (iterator.MoveNext())
// Console.WriteLine(iterator.Current);
//foreach (object obj in users)
// Console.WriteLine(obj);
//MyEnumrator myEnum = new MyEnumrator(new ArrayList() { "AAa", "bCCC" });
//如果实现了MoveNext方法,Current属性等,就可以通过下面的方法遍历对象集合;实际上,此时,刚好正是实现了接口IEnumerator。
//while (myEnum.MoveNext())
// Console.WriteLine(myEnum.Current);