in dotnet core we use lamda or delegate code example
Example 1: the key concepts of delegates, events and lambda expressions;
using System;
using System.Linq;
public class Program
{
public delegate string Reverse(string s);
static string ReverseString(string s)
{
return new string(s.Reverse().ToArray());
}
static void Main(string[] args)
{
Reverse rev = ReverseString;
Console.WriteLine(rev("a string"));
}
}
Example 2: c# delegate lambda
public class Program
{
public delegate double MathOperation(double x, double y);
static double Addition(double x, double y)
{
return x + y;
}
static double Multiply(double x, double y)
{
return x * y;
}
static double Subtration(double x, double y)
{
return x - y;
}
static void Main(string[] args)
{
MathOperation myFunction = Addition;
Console.WriteLine("The math gives: " + myFunction(2.6, 8.0));
MathOperation myFunction2 = Multiply;
Console.WriteLine("The math gives: " + myFunction2(7.9, 1.6));
MathOperation multiFunction = Addition + Multiply;
multiFunction += Subtration;
multiFunction(3.1, 4.5)
multiFunction -= Addition;
multiFunction(3.1, 4.5)
}
}
public class Program
{
public delegate double MathOperation(double x, double y);
static void Main(string[] args)
{
MathOperation myFunction = delegate(double x, double y) { return x + y; };
Display(myFunction);
}
static void Display(MathOperation myFoo)
{
Console.WriteLine("The math gives: " + myFoo(2.8, 1.5));
}
}
public class Program
{
public delegate double MathOperation(double x, double y);
static void Main(string[] args)
{
MathOperation myFunction = (double x, double y) => { return x + y; };
MathOperation myFunction2 = (x,y) => x * y;
}
}