using System; using System.Linq; using System.Collections.Generic; namespace Shand.Extension { public static partial class Extensions { public static bool IsEmpty<T>(this IEnumerable<T> source) { return source.Count() == 0; } public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach (T item in source) { action(item); } } public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> predicate) { return source.Where(predicate); } public static bool Contains<T>(this IEnumerable<T> source, Func<T, bool> predicate) { foreach (T item in source) { if (predicate(item)) { return true; } } return false; } } }
これは切実にほしかった
List<int> ls = new List<int>(); if (ls.IsEmpty()) { Console.WriteLine("Empty"); }
わりと必須かも(ForEach)
List<int> ls = new List<int>{3, 1, 4}; ls.Select(x => x + 1).ForEach(Console.WriteLine);
Where を Filter と書きたかっただけ
List<int> ls = new List<int>{3, 1, 4}; foreach (int item in ls.Filter(x => x > 1)) { Console.WriteLine(item); }
メソッドでの該当チェック
List<string> ls = new List<string> {"Akira", "Johnny", "Millia"}; if (ls.Contains(name => name == "Akira")) { Console.WriteLine("Found"); }