fork(2) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public static class ListExtensions
  6. {
  7. /// <summary>
  8. /// Finds the indices of all objects matching the given predicate.
  9. /// </summary>
  10. /// <typeparam name="T">The type of objects in the list.</typeparam>
  11. /// <param name="list">The list.</param>
  12. /// <param name="predicate">The predicate.</param>
  13. /// <returns>Indices of all objects matching the given predicate.</returns>
  14. public static IEnumerable<int> FindIndices<T>(this IList<T> list, Func<T, bool> predicate)
  15. {
  16. return list.Where(predicate).Select(list.IndexOf);
  17. }
  18. }
  19.  
  20. public static class Test
  21. {
  22. public static void Main()
  23. {
  24. var list = new List<int> { 10, 11, 23, 34, 56, 43 };
  25. var indices = list.FindIndices(value => value > 23);
  26.  
  27. foreach(var index in indices)
  28. {
  29. Console.WriteLine(index);
  30. }
  31. }
  32. }
Success #stdin #stdout 0.04s 34104KB
stdin
Standard input is empty
stdout
3
4
5