开发者

How is the long method way of doing below code in "Lambda Expressions" in the past?

开发者 https://www.devze.com 2023-02-08 07:39 出处:网络
public static IEnumerable<T> Get<T>(this IEnumerable<T> source, Func<T, bool> predicate)
public static IEnumerable<T> Get<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    {
        foreach (T item in source)
        {
            if 开发者_开发技巧(predicate(item))
                yield return item;
        }
    }


Like this:

public static IEnumerable<T> Get<T>
    (IEnumerable<T> source, Func<T, bool> predicate)
{
    var list = new List<T>();

    foreach (T item in source)
    {
        if (predicate(item))
        {
            list.Add(item);
        }
    }

    return list;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消