Action with Lambda Expression

PHOTO EMBED

Mon Sep 20 2021 13:34:16 GMT+0000 (Coordinated Universal Time)

Saved by @dejohnny #c

static void Main(string[] args)
{
    Action act = () =>
    {
        Console.WriteLine("No Parameter");
    };

    Action<int> actWithOneParameter = (arg1) =>
        {
            Console.WriteLine("Par: " + arg1);
        };

    Action<int, int> actWithTwoParameter = (arg1, arg2) =>
        {
            Console.WriteLine("Par1: " + arg1 + ", Par2: " + arg2);
        };

    act();
    actWithOneParameter(1);
    actWithTwoParameter(1, 2);
}
content_copyCOPY

A delegate in C# represents a reference type that encapsulates a method. When we declare a delegate type we specify some parameters and a return type. We can only store those methods in delegates which has same parameters and same return type. Microsoft introduced some pre-built delegates so that we don't have to declare delegates every time. Action is one of the pre-built delegates. Action in C# represents a delegate that has void return type and optional parameters. There are two variants of Action delegate. Action Action<in T>