Lambda Func Action

Key points

				
					using System;

namespace ConsoleApp9
{
    class Program
    {
        delegate decimal MultiplyByFive(int x);
        //extension method ...delegate ...generic delegate...
        //extension method on MyFind...

        static void Main(string[] args)
        {
            Console.WriteLine("----------- STRING EXTENSION METHOD----------------------");

            var str = "Anurag";
            Console.WriteLine(str.Length);//6
            Console.WriteLine(str.LengthByTwo());//3

            Console.WriteLine("-----------DELEGATE REFERENCING A METHOD----------------------");

            MultiplyByFive obj1 = Mulby5;
            Console.WriteLine(obj1.Invoke(5));

            Console.WriteLine("--------------DELEGATE ANOYMOUS METHOD-------------------");


            MultiplyByFive obj2 = new MultiplyByFive(delegate (int x) { return x * 5; });
            Console.WriteLine(obj2.Invoke(5));

            Console.WriteLine("--------------DELEGATE INLINE-----------------");


            MultiplyByFive obj3 = x => x * 5;
            Console.WriteLine(obj3.Invoke(5));

            Console.WriteLine("-------------FUNC DELEGATE--------------------");


            /*Func Action...*/

            Func<int, decimal> func1 = x => x * 5;
            Console.WriteLine(func1(5));

            Console.WriteLine("--------------ACTION DELEGATE----------------------");

            Action<int> action1 = x =>
            Console.WriteLine($"input is {x} but returning always void");
            action1(5);

            
            Console.ReadLine();


        }

        public static decimal Mulby5(int num)
        {
            return num * 5;
        }
    }

    static class StringExtension
    {
        public static int LengthByTwo(this string s)
        {
            return s.Length / 2;
        }
    }
}
				
			

———– STRING EXTENSION METHOD———————-
6
3
———–DELEGATE REFERENCING A METHOD———————-
25
————–DELEGATE ANOYMOUS METHOD——————-
25
————–DELEGATE INLINE—————–
25
————-FUNC DELEGATE——————–
25
————–ACTION DELEGATE———————-
input is 5 but returning always void

Leave a Comment