好的,我刚刚在研究IEnumerator
和IEnumerable
。现在MSDN说这些东西的主要目的是遍历客户端集合。
好吧,我可以遍历自定义集合而不使用任何一个(或者至少我认为如此)。
namespace ienumerator
{
class person
{
public person(string first, string second)
{
fname = first;
lname = second;
}
public string fname { get; set; }
public string lname { get; set; }
}
class person_list
{
public person[] list;
public person_list(person[] per)
{
list = new person[per.Length];
for (int i = 0; i < per.Length; i++)
{
list[i] = per[i];
}
}
}
class Program
{
static void Main(string[] args)
{
person[] names = new person[3]
{
new person("some","one"),
new person("another","one"),
new person("sss","ssdad"),
};
person_list list_of_names = new person_list(names);
int i = 0;
while (i < list_of_names.list.Length)
{
Console.WriteLine(list_of_names.list[i].fname);
i++;
}
Console.Read();
}
}
}
那么我的理解有什么问题呢?上述内容给了我使用 IEnumerator
和 IEnumerable
实现后得到的相同结果。
LinkedList
并尝试编译它。 - tukaefint i
是一个简单(且暴露)的迭代器。 - H H