how to invoke a static method using c# lambda expressions
To invoke a static method, you can use a static method reference with a lambda expression. Static method reference separates the class name and method name with a double colon (::), as shown below:
ClassName::StaticMethodName
Here is an example demonstrating how to call a static method in a lambda expression.
using System;
public class Program
{
public static void Main()
{
Func<int, int, int> add = Calculator.Add;
int result = add(5, 3);
Console.WriteLine(result);
}
}
public static class Calculator
{
public static int Add(int a, int b)
{
return a + b;
}
}
In the example above, we used a static method reference Calculator.Add in a lambda expression to call a static method. We then assigned this lambda expression to a Func
The output result is 8.