sheikh-k
11/3/2016 - 1:13 PM

Lambda Expression (Basic++).cs

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

namespace Lambda_Expression
{
    class Book
    {
        public string Author { get; set; }
        public string Name { get; set; }
        public DateTime Published { get; set; }
    }
	
    class Program
    {
        static void Main(string[] args)
        {
            /*==================================== LAMBDA EXPRESSION (Functional Programming)==================================*/
            //EXAMPLE:1
            Func<int, int> func1 = x => x*2; //function defination: Func<returnType, param_types_one_by_one> functionName = (parameters_one_by_one) => expression/code_snippet;
            Console.WriteLine(func1.Invoke(5)); //invoking or calling function

            Func<double, int, double> func2 = (x, y) => 2*(x + y);
            Console.WriteLine(func2.Invoke(5,10));

            Func<int, int, int, int> func3 = (i, j, k) =>
            {
                int temp = 2*(i + j);
                return temp + k;
            };
            Console.WriteLine(func3.Invoke(5,10,20));
            Console.WriteLine();

            //EXAMPLE:2
            List<string> names=new List<string>() { "Kawser", "Rasel", "Moshiur"};
            string found = names.Find(n => n.StartsWith("R")); //snippets inside Find() method is lambda expression (NOTE: Find method want Predicate Type which is a lambda expression in fact
            //found = names.Find(n => n == "Moshiur");
            //Console.WriteLine(names.Exists(n => n=="Kawser"));
            Console.WriteLine(found);


            //EXAMPLE:3
            List<double> values=new List<double>() {30,40,60,70,80,90,100};
            double aNumber = values.Find(number => (number >= 50)); //finds the first occurrence of a number which is >=50 from the values list
            Console.WriteLine(aNumber);

            List<Book> bookList=new List<Book>()
            {
                new Book() { Author = "Humayun Ahmed", Name = "Captain-Q & Stranger from Unknown", Published = new DateTime(1993,05,20)},
                new Book() { Author = "Taslima Nasrin" , Name = "Amar Meyebela", Published = new DateTime(1996,06,15)},
                new Book() { Author = "Kaji Najrul", Name = "Bidrohi Kobita", Published = new DateTime(1995,12,17)}
            };

            var selectedBooks = bookList.FindAll(book => (book.Published >= new DateTime(1994, 01, 01)));
            Console.WriteLine("Books of 1994 or later: ");
            foreach (var aBook in selectedBooks)
            {
                Console.WriteLine(aBook.Author+", "+aBook.Name+", "+Enum.GetName(typeof(Months),aBook.Published.Month)+"-"+aBook.Published.Year);
            }

            Console.ReadKey();
        }

        private enum Months
        {
            January=1,
            February,
            March,
            April,
            May,
            June,
            July,
            August,
            September,
            October,
            November,
            December
        };
    }
}