3. Using Predicate (Creating delegate function)
Creating a function which will check the condition and return the result.
bool isOdd(int n)
{
if (arg% 2 != 0) { return true; }
else { return false; }
}
Calling that function name inside the method that accepts predicate as an argument.
lstNumbers.RemoveAll(isOdd);
Or
instead of the above line we can create a predicate object which refers to the isOdd() function. And we can use this object to filter out the condition by specifying it in the argument of method call.
Predicate<int> predEven = new Predicate<int>(isOdd);
lstNumbers.RemoveAll(predEven);
Advantages : In the last approach your code will be elegant and reusable.
Some more function which use predicate as arguments are :
List.Exists
List.Find
List.TrueForAll
These function check, if the condition defined in the predicate is matching or not and return the result accordingly.