Guest User

Untitled

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. foreach (Foo f in fooList) {
  2. if (f.Equals(fooTarget)) {
  3. return f;
  4. }
  5. }
  6.  
  7. fooList.Find(delegate (Foo f) { return f.Equals(fooTarget); });
  8.  
  9. fooList.Find(f => f.Equals(fooTarget));
  10.  
  11. public T Find(Predicate<T> p)
  12. {
  13. //iterate through the collection and return the first match
  14. IEnumerator<T> enumerator = this.GetEnumerator();
  15.  
  16. while (enumerator.MoveNext())
  17. {
  18. if (p(enumerator.Current))
  19. {
  20. return enumerator.Current;
  21. }
  22. }
  23.  
  24. return default(T);
  25. }
  26.  
  27. Predicate<string> findShortNames1 = x => x.Length == 3; // lambda expression
  28. Predicate<string> findShortNames2 = delegate(string x) { return x.Length == 3; }; // anonymous method
  29. Predicate<string> findShortNames3 = MatchOnShortNames; //existing method
  30.  
  31. // ...
  32. private bool MatchOnShortNames(string s)
  33. {
  34. return s.Length == 3;
  35. }
  36.  
  37. someList.FindAll(findShortNames1);
  38.  
  39. List<Employee> retiredEmployees = new List<Employee>();
  40. foreach (Employee employee in EmployeeList)
  41. {
  42. if (employee.IsRetired)
  43. retiredEmployees.Add(employee);
  44. }
  45.  
  46. retiredEmployees = EmployeeList.FindAll(e => e.IsRetired);
  47.  
  48. public class Employee
  49. {
  50. private bool _isRetired;
  51. private double _amount;
  52. public double GetPayAmount()
  53. {
  54. if (_isRetired)
  55. return _amount * 0.9;
  56. else
  57. return _amount;
  58. }
  59. }
  60.  
  61. public interface IEmployee
  62. {
  63. double GetPayAmount();
  64. }
  65.  
  66. public class Employee : IEmployee
  67. {
  68. private double _amount;
  69. public double GetPayAmount()
  70. {
  71. return _amount;
  72. }
  73. }
  74.  
  75. public class RetiredEmployee : IEmployee
  76. {
  77. private double _amount;
  78. public double GetPayAmount()
  79. {
  80. return _amount * 0.9;
  81. }
  82. }
  83.  
  84. int index = this.listObjects.FindIndex(x => x.PropertyA == objectItem.PropertyA);
  85.  
  86. List<ClassA> listClass = new List<ClassA>();
  87.  
  88. //... some more code filling listClass with ClassA types ...
  89.  
  90. ClassA tempClassA = listClass.FirstOrDefault().Where(x=> x.PropertyA == someValue);
Add Comment
Please, Sign In to add comment