using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 代码清单4._6
{
public class Employees : IEnumerable
{
public ArrayList EmployeeNames;//数组列表
public Employees()
{
EmployeeNames = new ArrayList();//实例化数组对象
}
public void Add(string name)
{
EmployeeNames.Add(name);
}
public void Remove(string name)
{
EmployeeNames.Remove(name);
}
public IEnumerator GetEnumerator()
{
return new EmployeesEnumerator(this);
}
}
public class EmployeesEnumerator : IEnumerator
{
private int position = -1;
private Employees employees;
public EmployeesEnumerator(Employees es)
{
this.employees = es;
}
public bool MoveNext()
{
if (position < employees.EmployeeNames.Count - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
return employees.EmployeeNames[position];
}
}
}
class Program
{
static void Main(string[] args)
{
Employees e = new Employees();
e.Add("张三");
e.Add("李四");
e.Add("王五");
e.Remove("李四");
foreach(string i in e)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 代码清单4._6
{
public class Employees : IEnumerable
{
public ArrayList EmployeeNames;//数组列表
public Employees()
{
EmployeeNames = new ArrayList();//实例化数组对象
}
public void Add(string name)
{
EmployeeNames.Add(name);
}
public void Remove(string name)
{
EmployeeNames.Remove(name);
}
public IEnumerator GetEnumerator()
{
return new EmployeesEnumerator(this);
}
}
public class EmployeesEnumerator : IEnumerator
{
private int position = -1;
private Employees employees;
public EmployeesEnumerator(Employees es)
{
this.employees = es;
}
public bool MoveNext()
{
if (position < employees.EmployeeNames.Count - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
return employees.EmployeeNames[position];
}
}
}
class Program
{
static void Main(string[] args)
{
Employees e = new Employees();
e.Add("张三");
e.Add("李四");
e.Add("王五");
e.Remove("李四");
foreach(string i in e)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
解决方案 »
- 多线程相关问题
- 求好心人给个简单的WEB OA系统源代码!!
- 问个关于文件传输的问题
- asp如何在数据访问层(DAL)编写函数向数据库插入和读取图片
- 线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
- DataGrid表头问题,要晕了
- 想从asp.net(c#)转到c#(c/s)上,我做了一个程序怎么(方法名都有的不一样呀)/晕/指路/
- 大家经常分发源码,我写了个分析网页信息的工具软件,可以直接分析出email,方便大家
- 怎样才能卸载MDAC呢?
- ★★★高分相送!谁有bitware新版本(不要3.3版),绝对能装到win2000上的!在线等待!★★★
- 关于访问数据库以下3种设计方法的比较
- 问下webform何时断开数据库连接?
Employees : IEnumerable//实现foreach目的就是让Employees可以使用foreach来循环迭代