c sharp

C# Action

If you have ever used pointers in any programming language, then you may have an idea about the referencing. C# Delegates work the same as pointers do. Just like we reference the functions using pointers, we can use delegates to reference the function in C#. The delegates are purposely built to create call-back functions and adding events. This means a delegate can decide which method will be executed at which time in C#. Delegates help us encapsulate the functions and can execute more than two functions for a solo event. Delegates have custom and action types. In this guide, we have decided to cover both its types and especially the “Action” type. Let’s get started with the C# file creation in the shell to take a look at delegates.

Example # 01:

Without taking a look at the custom delegate, we cannot understand the importance of the Action delegate in C#. Within this example, we will be discussing the custom delegate first. we have been using the “System” library in the code with the keyword “using”. This library is a must-have in every C# code because it makes us use many standard use functions and statements in the C# code.

Without this library, our code has no use for us because it will not show the required results. We have initialized a new class named “Test” with two functions in it. One of those functions is the Main() function of C# code and it is a must-have in every code. Without this function, no other parts of the C# code get executed. Another function is the user-defined “Calculate” function, which will be used to perform some calculations on variables. The execution starts from the Main() method.Before that, we have to describe the declaration of a custom delegate that is defined in the first line of the class Test.

We have defined a delegate named “custom” having two arguments of integer type (i.e. x and y), referencing the same name and type argument function in the code. This delegate is of “public” access modifier and initiated with the keyword “delegate” followed by the “void” type. As the Calculate function has the same name and types of arguments in it, this delegate must be referencing the “Calculate” function. Within the main() method, we have created an object “a” of delegate “custom” to reference the “Calculate” function. Now, to pass values to the “Calculate” function, we will be using the object “a” of the custom delegate (i.e. 14, and 7.) Now, when a value is passed, the Calculate function will be executed using these values. There are 4 Console.WriteLine() function statements are used in the Calculate function to perform different operations (i.e. the sum and subtraction of 2 values, multiplication of 2 values, and division of two values using x and y.)

This is all about the use of custom delegate in C#. Let’s now compile this C# code in the terminal of Linux with the help of a C# “mcs” compiler. After compiling it, the compiler will create an “exe” file for execution. Now, we need to use this “exe” file within the terminal with the “mono” instruction to execute our code. It turns out that the delegate works, like a class object, is referenced towards the Calculate function to pass its values and calculate sum, subtraction, multiplication, and division result i.e. 21, 7, 98, and 2 respectively.

Example # 02:

The Action delegate saves us from defining the custom delegate in the code and it doesn’t return a value. Also, it reduces the size of a code as well. Let’s say, how we can use an action delegate in this example code? So, we have been starting the same code structure of C# here using the “System” library, and the “Test” class in it. After that, we have added 2 functions (i.e. user-defined Calculate function and the built-in Main() function.)

Within the Calculate function, we have been using the same 4 Console.WriteLine() function statements to calculate the sum, subtraction, multiplication, and division value of variables “x” and “y” of integer type. While the Main() function contains the initialization of an Action delegate “a” with the keyword “Action” followed by the “<>” characters specifying the return types for parametric values passed by this delegate to the Calculate function. Now, the Calculate function becomes the reference of Action delegate “a” and we have used it to pass two values 7 and 14 to the function “Calculate” without calling it. You can see that we don’t have to declare an action delegate like we did for custom and it has also reduced the size of a code a little bit. Let’s save this code and use it on execution.

After the compilation of this code and execution of its “exe” file, we have got the same result for sum and multiplication while a little different for subtraction and division. Both custom and action delegates work quite similarly but we have to define the custom delegate first.

Example # 03:

Let’s see how an action delegate reacts when we use a return statement in the code. So, we have updated our code and added the Calculate and Main() function to it. Within the Main() of this code, we have created an action delegate “a” of two integer parameters for referencing the Calculate function. An action delegate object “a” is used to pass two integers to the Calculate function without calling the Calculate function and initialized an integer “s” to get the calculated result back in the main().

The Console.WriteLine() function has been used to display the returned value from the Calculate function to display on our shell screen. Within the Calculate function, we have initialized an integer variable “sum” calculating the sum of two values by using two variables “x” and “y”. After calculating the sum, we have returned the “sum” variable to the main() function. Now, we expect that it gives us an error on compilation.

So, after compiling the “delegate.cs” file with the compiler of C#, we have an error.

Let’s update the code to see the different use of action delegate. So, we have added a simple Main() function within the Test class. We have been initializing an action delegate “a” within it followed by the parameter of string type. To reference some string variable, we need to use the keyword delegate followed by the name of a variable and its type in parameters (i.e. “s”.) The Console.WriteLine() statement will be displaying the value of a string. Outside the Main() function, we have called the action delegate and passed it a string value “Hello World”.

After compiling and executing our code, it displayed our string using the action delegate “a”.

Conclusion:

This article demonstrates the concept of delegates with some of its examples. We have studied the use and structure of custom delegates so that we can compare them with the action delegates. Also, we have discussed some examples for the use of action delegates and explained how the action delegates are different from the custom delegates, how they can reduce the size of a code, and how it reacts to the return statement usage.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.