【发布时间】:2016-10-24 13:08:36
【问题描述】:
请原谅我这是一个菜鸟问题。我是 C# 新手,对 IEnumerables 的使用感到困惑。我正在尝试遍历对象列表,但我熟悉的普通 foreach 方法可能由于显而易见的原因不起作用。谁能给我一个实现代码的示例,该代码使用 IEnumerable 遍历对象列表?
编辑
class MyObjects : IEnumerable<LIST>
{
List<LIST> myTeam = new List<GroupMember>();
public LIST this[int index]
{
get { return myTeam[index]; }
}
public IEnumerator<LIST> GetEnumerator()
{
return myTeam.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator<GroupMember> IEnumerable<GroupMember>.GetEnumerator()
{
throw new NotImplementedException();
}
}
以上代码是我目前实现的。我尝试使用 foreach 遍历返回的列表,但它没有用,进一步的研究表明我必须将列表明确定义为可枚举等。我只是不知道如何实现。是的,当您不知道自己在做什么时,没有什么是显而易见的。
所以,总而言之,我可以调用一个返回对象列表的函数。我希望能够遍历该对象列表,以便可以访问其中的值。
【问题讨论】:
-
显示您的代码无法按预期工作。
-
由于 IEnumerable 包含在 System.Collection 中,只需添加一个 using 指令
-
“由于可能明显的原因不起作用”。如果我们不知道您在做什么,那么一切都是显而易见的。
-
编辑了问题。
标签: c# list ienumerable