c# anonymous function code example
Example 1: c# anonymous class
var myObject = new { Foo = "foo", Bar = 1, Quz = 4.2f }
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;
}
}