Lambda Expressions in C#

Now we are going to learn about Lambda expressions in c#.
  • 5537

Introduction

A lambda expression make program easier, lambda expression is a anonymous function by creating delegates. we can pass local function by the value of the function calls.

Example

namespace ConsoleApplication2
  {
    delegate bool D();
    delegate bool D2(int i);
  class Test
 
{
  D
del;
  D2 del2;
   public void TestMethod(int input)
   {
   int j = 0;
   del = () => { j = 10; return j > input; };
   del2 = (x) => { return x == j; };
   Console.WriteLine("j = {0}", j); 
   bool boolResult = del();
   Console.WriteLine("j = {0}. b = {1}", j, boolResult);
    }
      static void Main()
        {
            Test test = new Test();
            test.TestMethod(5);
            bool result = test.del2(10);
            Console.WriteLine(result);
            Console.ReadKey();
         }
    }
    }

Output

lambdaexpression.jpg 

© 2013 dotNetheaven. All rights reserved.