如何在ObservableCollection< T>上执行foreach lambda表达式? [英] How do I execute a foreach lambda expression on ObservableCollection<T>?

查看:136
本文介绍了如何在ObservableCollection< T>上执行foreach lambda表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ObservableCollection< T>上执行foreach lambda表达式?

How do I execute a foreach lambda expression on ObservableCollection<T>?

尽管ObservableCollection< T>中没有foreach方法,但是List< T>中存在该方法.

There is not method of foreach with ObservableCollection<T> although this method exists with List<T>.

有没有可用的扩展方法?

Is there any extension method available?

推荐答案

BCL默认没有可用的方法,但是直接编写具有相同行为的扩展方法(为简洁起见,省略了参数检查)

There is no method available by default in the BCL but it's straight forward to write an extension method which has the same behavior (argument checking omitted for brevity)

public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) {
  foreach ( var cur in enumerable ) {
    action(cur);
  }
}

用例

ObservableCollection<Student> col = ...;
col.ForEach(x => Console.WriteLine(x.Name));

这篇关于如何在ObservableCollection&lt; T&gt;上执行foreach lambda表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆