Lambda exp, Func generic delegate, Action generic delegate, and Predicate generic delegate

Lambda expression, Funct generic delegate, Action generic delegate, and Predicate generic delegate make your delegate code more simple.

Lambda expression helps to build expression tress -> LINQ, BL (Business Language).

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Linq;
using System.Linq.Expressions;

namespace ConsoleApplication2
{
    class Program
    {
        //Lambda expression, Funct generic delegate, Action generic delegate, and Predicate generic delegate make your delegate code more simple
        //Lambda expression helps to build expression tress -> LINQ, BL (Business Language)
        delegate double delCal(int r);
        delegate int delSum(int x, int y);
        static void Main(string[] args)
        {
            //1)
            /**********Lambda expression, Funct generic delegate, Action generic delegate, 
            and Predicate generic delegate make your delegate code more simple************/

            //Simple delegate
            delCal dc = CalArea;
            double Area = dc.Invoke(20);
            //End

            //Anonymous function
            delCal dCal = new delCal(delegate(int r){return 3.14 * r * r;
            }
            );
            double Area2 = dCal.Invoke(20);
            //End

            //Lambda expression -> (input parameters) => expression
            delCal dCalL = r => 3.14 * r * r;
            double Area3 = dCalL.Invoke(20);

            delSum delsum = (x, y) => { x = x + 10; y = y + 10; return x + y; };
            int sum = delsum.Invoke(10, 10);
            //End

            //Lambda + Func => Func is used when we want to pass input and get the out of any type.
            Func delFun = r => 3.14 * r * r;
            double delFunArea = delFun.Invoke(20);
            //End
            
            //Action => It take input but doesn't return output.
            Action oAction = O => Console.WriteLine(O);
            oAction.Invoke("Hello World");
            //End
            
            //Predicate => Used when function should return either True or False value to take further decision or action.
            Predicate oPredicate = O => O.Length > 4;
            bool bl = oPredicate.Invoke("Izhar");
            //End

            //2)
            /**************Lambda expression helps to build expression tress -> LINQ, BL (Business Language)************/

            //Example: (10+15)-(10+5)
            //http://www.codeproject.com/Articles/19154/Understanding-LINQ-C
            BinaryExpression be1 = Expression.MakeBinary(ExpressionType.Add, Expression.Constant(10), Expression.Constant(15));
            BinaryExpression be2 = Expression.MakeBinary(ExpressionType.Add, Expression.Constant(10), Expression.Constant(5));
            BinaryExpression be = Expression.MakeBinary(ExpressionType.Subtract, be1, be2);

            int intResult = Expression.Lambda<Func>(be).Compile()();
            //End
        }

      
        static double CalArea(int r) {
            return 3.14 * r * r;
        }
    }
}
Advertisements

#action-generic-delegate, #and-predicate-generic-delegate, #func-generic-delegate, #lambda-exp