{
return null;
}
else
{
return name[index];
}
}
}
public bool MoveNext()
{
++index; //每调用此方法就将索引往下+1
if (index<name.Length)
{
return true;
}
else
{
return false;
}
}
public void Reset()
{
index=-1;
}
}
在主方法里面:
复制代码 class Program
{
static void Main(string[] args)
{
Person p = new Person();
//for (int i = 0; i < p.Count; i++)
//{
// Console.WriteLine(p[i]);
//}
foreach (string item in p)
{
Console.WriteLine(item);
}
//实际执行foreach就相当于执行下面几句话:
Console.WriteLine("==================================================");
IEnumerator p1 = p.GetEnumerator();
while (p1.MoveNext())
{
string str=(string)p1.Current;
Console.WriteLine(str);
}










