Thursday 27 February 2014

Lamda Expression In LINQ

Lamda Expression In LINQ :

Lambda expressions are similar to anonymous methods introduced in C# 2.0, except that lambda expressions are more concise and more flexible. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters and the right side holds the expression or statement block.

For Anonymos Funcation, use the below link :
http://computerauthor.blogspot.in/2011/08/c-interview-questions-what-are.html


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

namespace ConsoleApplication1
{
    class Program
    {
        delegate int PointToDelegate(int x, int y);

        static void Main(string[] args)
        {

            //Console.WriteLine(Convert.ToString(Add(10, 5)));


            // WithOut Use Of Anonymos Funcation

            PointToDelegate objFun = Add;
            int reslt1 =objFun(5, 5);
            Console.WriteLine(Convert.ToString(reslt1));

           
           
            // With the Anonymos Funcation
            PointToDelegate objFunWithAny = delegate(int m, int n)
                                            {
                                                Console.WriteLine("This is Delegate Inline Defination");
                                                return m + n;
                                            };

            int reslt2 = objFunWithAny(5, 7);
            Console.WriteLine(Convert.ToString(reslt2));
           
           
            // With the Lamda Expression

            Func<int, int, int> func1 = (x, y) => { return x + y; };
            int reslt3= func1.Invoke(10, 9);
            Console.WriteLine("This is Using the Lambda Expression");
            Console.WriteLine(Convert.ToString(reslt3));
           
            Console.WriteLine();
        }


        static int Add(int a, int b)
        {
            Console.WriteLine("This is Add Funcation");

            return a + b;
        }

       
    }
}