显然有许多方法可以迭代集合。很好奇是否有什么不同,或者为什么你用一种方式而不是另一种。
第一类型:
List<string> someList = <some way to init>
foreach(string s in someList) {
<process the string>
}
其他方式:
List<string> someList = <some way to init>
someList.ForEach(delegate(string s) {
<process the string>
});
我想,除了我上面使用的匿名委托,你还可以指定一个可重用的委托。
为了好玩,我将List弹出到reflector中,结果是c#:
public void ForEach(Action<T> action)
{
if (action == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
}
for (int i = 0; i < this._size; i++)
{
action(this._items[i]);
}
}
类似地,foreach使用的枚举器中的MoveNext是这样的:
public bool MoveNext()
{
if (this.version != this.list._version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
if (this.index < this.list._size)
{
this.current = this.list._items[this.index];
this.index++;
return true;
}
this.index = this.list._size + 1;
this.current = default(T);
return false;
}
列表中。ForEach比MoveNext更精简——处理更少——将更有可能JIT成高效的东西。
此外,foreach()将分配一个新的Enumerator。GC是你的朋友,但如果你重复做同样的foreach,这将产生更多的一次性对象,而不是重用同一个委托- but -这确实是一个边缘情况。在典型用法中,您将看到很少或没有区别。
ForEach函数是泛型类List的成员。
我已经创建了以下扩展来复制内部代码:
public static class MyExtension<T>
{
public static void MyForEach(this IEnumerable<T> collection, Action<T> action)
{
foreach (T item in collection)
action.Invoke(item);
}
}
因此,最后我们使用普通的foreach(如果你愿意,也可以使用循环for)。
另一方面,使用委托函数只是定义函数的另一种方式,以下代码:
delegate(string s) {
<process the string>
}
等价于:
private static void myFunction(string s, <other variables...>)
{
<process the string>
}
或者使用labda表达式:
(s) => <process the string>