yield 关键字

第一次遇到,标记一下.

上段代码应该都明白了怎么用

public static IEnumerable<T> FindWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
{
    if (collection == null)
        throw new ArgumentNullException("collection");
    if (predicate == null)
        throw new ArgumentNullException("predicate");

    foreach (T item in collection) {
        if (predicate(item)) {
            yield return item;
        }
    }
}

 

貌似是用于返回对象是IEnumerable或IEnumerable<T>的

posted @ 2007-06-20 14:36  Anders06  阅读(281)  评论(3编辑  收藏  举报