C# Program to Illustrate Actions
Posted by Superadmin on August 13 2022 07:47:16

C# Program to Illustrate Actions

 

This is a C# Program to illustrate actions.

Problem Description

This C# Program Illustrates Actions.

Problem Solution

Here action objects return no values.

Program/Source Code

Here is source code of the C# Program to Illustrate Actions. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Actions
 */
using System;
 
class Program
{
    static void Main()
    { 
        Action<int> action1 =(int x) => Console.WriteLine("OUTPUT {0}", x);
        Action<int, int> action2 =(x, y) => Console.WriteLine("OUTPUT {0} and {1}", 
                                                                x, y);
        action1.Invoke(1111);
        action2.Invoke(200, 3000);
        Console.Read();
    }
}
Program Explanation

This C# program is used to illustrate actions. We are creating two objects ‘action1’ and ‘action2’ using Action<int>. The Action<int> is used to encapsulate a method that has a single parameter and does not return a value. Here Action objects return no values. Pass the values of ‘action1’ and ‘action2’ variables as an argument to Invoke().

 
Runtime Test Cases
 
OUTPUT 1111
OUTPUT 200 and 3000