C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: We validate the argument using two if-statements. This cannot be done using a lambda expression.
Finally: A boolean expression is evaluated, causing the anonymous function to return true if the argument is greater than one.
C# program that uses anonymous method
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> values = new List<int>() { 1, 1, 1, 2, 3 };
// This syntax enables you to embed multiple statements in an anonymous function.
List<int> res = values.FindAll(delegate(int element)
{
if (element > 10)
{
throw new ArgumentException("element");
}
if (element == 8)
{
throw new ArgumentException("element");
}
return element > 1;
});
// Display results.
foreach (int val in res)
{
Console.WriteLine(val);
}
}
}
Output
2
3
Tip: To see examples of lambda expressions on the Find method, please visit the relevant article on this website.
List Find, FindAllTip: You can also find copies of the C# specification in PDFs on the Internet. But these may lack annotations.