Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (Foo f in fooList) {
- if (f.Equals(fooTarget)) {
- return f;
- }
- }
- fooList.Find(delegate (Foo f) { return f.Equals(fooTarget); });
- fooList.Find(f => f.Equals(fooTarget));
- public T Find(Predicate<T> p)
- {
- //iterate through the collection and return the first match
- IEnumerator<T> enumerator = this.GetEnumerator();
- while (enumerator.MoveNext())
- {
- if (p(enumerator.Current))
- {
- return enumerator.Current;
- }
- }
- return default(T);
- }
- Predicate<string> findShortNames1 = x => x.Length == 3; // lambda expression
- Predicate<string> findShortNames2 = delegate(string x) { return x.Length == 3; }; // anonymous method
- Predicate<string> findShortNames3 = MatchOnShortNames; //existing method
- // ...
- private bool MatchOnShortNames(string s)
- {
- return s.Length == 3;
- }
- someList.FindAll(findShortNames1);
- List<Employee> retiredEmployees = new List<Employee>();
- foreach (Employee employee in EmployeeList)
- {
- if (employee.IsRetired)
- retiredEmployees.Add(employee);
- }
- retiredEmployees = EmployeeList.FindAll(e => e.IsRetired);
- public class Employee
- {
- private bool _isRetired;
- private double _amount;
- public double GetPayAmount()
- {
- if (_isRetired)
- return _amount * 0.9;
- else
- return _amount;
- }
- }
- public interface IEmployee
- {
- double GetPayAmount();
- }
- public class Employee : IEmployee
- {
- private double _amount;
- public double GetPayAmount()
- {
- return _amount;
- }
- }
- public class RetiredEmployee : IEmployee
- {
- private double _amount;
- public double GetPayAmount()
- {
- return _amount * 0.9;
- }
- }
- int index = this.listObjects.FindIndex(x => x.PropertyA == objectItem.PropertyA);
- List<ClassA> listClass = new List<ClassA>();
- //... some more code filling listClass with ClassA types ...
- ClassA tempClassA = listClass.FirstOrDefault().Where(x=> x.PropertyA == someValue);
Add Comment
Please, Sign In to add comment