Lambda Expressions in C#

Hi Programmers, Welcome to new article of C#.net. This post i’ll write the Program for Lamda Expression in C# Console application. Lamda Expression : It is convenient way of defining an anonymous (unnamed) function.
It has two forms 1. Expression lamda : (input-parameter) => expression 2.Statement lamda : (input-parameter) => { }
. The Lamda Expression uses Linq namespace. Lamda expression has so many function such as Func,FindAll,Select, Where etc.. Let’s see the codes.

Directly use below codes into Editor.

using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
    class Program {
        static void Main(string[] args) {

            Func<int, int> fun = i => i * i;
            Console.WriteLine("Lamda Expression Example First = " + fun(5));

            int[] n = { 2, 3, 4 };
            var v1 = n.Select(j => j * j);
            Console.WriteLine(" Lamda Expression Example Second = " +string.Join(" ", v1));

            int[] age = { 19, 27, 8 };
            int cnt = age.Where(n => n > 18).Count();
            Console.WriteLine("{0} People's age Greater Than 18 Years ", cnt);

            List<int> l1 = new List<int>() { 1, 2, 3, 4, 5 };
            List<int> k = l1.FindAll(x => (x % 2) == 0);
            foreach (var k1 in k)
                Console.Write("{0}", k1);
        }
    }
}

Happy Coding…Thanks.

Post Author: adama