c sharp

C# Func

Func in the C# program is a delegate type. You usually know that delegating will help us to point out methods. In C# programming, we have built-in generic delegate types, such as “Func”. “Func” is included in system namespaces in the C# program. It contains zero or multiple input parameters and only one output parameter. The parameter on the last is treated as an output parameter. The “Func delegate” type can include input parameters from 0 to 16. The “Func” has a return type. In this tutorial, we will learn how to create “Func” in C# programming in Ubuntu 20.04. We will explain this “Func” in the C# program with the help of different examples. Now, look at these examples given below in this tutorial.

Example # 1: Func With Two Input Parameters and One Output Parameter

In this example, we use “Func”. This “Func” contains two input parameters. For the result of these two parameters, we have one parameter for the output result. Now, look at this example and how it works. We perform this example in Ubuntu 20.04 text editor:

In this code, we are using the library, which is “using System”. This “using System” will helps us for getting classes and functions that are useful for us in our C# program. We can easily access the “Console” and “WriteLine” using this library. In this code, we have a namespace with “CSharpProgram”. This “namespace” keyword will define a scope with a set of associated objects. For organizing the elements of C# code, the “namespaces” are used.

After this, we have a class that is kept public, and the name of this public class is “Function” in this code. We are declaring a “static int” method with a simple sum operation and passing two parameters, “int a” and “int b”. This sum operator will return the sum of these two parameters by using the “return a + b” statement. After this, we have the “main” method, which is static and written as “static void Main(String [ ] args)” in this C# code. In this “Main” method, the arguments are denoted by using “(String [ ] args)”. In the following line, we will create our “Func” to call our “sum” operation. Here, you can see that we have three integers.

Now, you see “Func<int, int, int> add = sum” represents that this “Func” has three parameters, two for input and one for output, and we define their type here as “int”. The input data is of integer type. The output is also an integer data type and assigns “sum” to “add”. Now, we print one line before calling the function. We are printing “The number is 5 and 8” using Console.WriteLine. We use this statement of code “add(5,8)” to get the final result. As “Func” holds the method “sum”, we pass two parameters here, and it will return the result of these two parameters.

Now, for the output in Ubuntu 20.04, we will write the commands on the terminal of Ubuntu 20.04. Our first command is the “mcs” command with the filename and the extension of “.cs”. When we run this command, an executable file is generated. Then, we write the “mono” command with the filename and extension “.exe”. Next, press “ENTER”. So, we get the output of our code. We also show you the output of the code in the following image. This image helps you to understand better how to get output in Ubuntu 20.04.

Now, see that in this output. It prints the line and then prints the sum of the numbers, which we pass as a parameter to our “Func” and returns a result that is the sum of these numbers and an integer.

Example # 2: For Returning the Square and Cube of a Number Using “Func”

In this code, we explain how to get the square and cube of an integer with the help of “Func”. Write this given code on the text editor and then execute the code. You will see how it gives you the result.

In the first line of code, we have the “using System” library for accessing classes and functions in the C# program. Then, we have a namespace with the name “Program” here. We discuss this in detail in the previous code. We are declaring a “public class” named “CSharp”. After that, we invoked a “main” function. The “static void Main (String [] args)” is the main method in this C# code. This method is static, and we use “(String [] args)” with this method which is used for representing the arguments. We are printing a line inside this class, “The number is 5”, using “Console.WriteLine”.

When we want to display something on the output screen, we use this “Console.WriteLine” method in C# programming. Now, we create a “Func” and pass two input parameters as “Func<double, double>”. Next, we need to define a name that is “square” here, which is equal to the “Square” method. We want to show the output, and for this, we have “Console.WriteLine”. Inside this, we define the “Func” name, which is “square”, and define the parameter here of double data type.

After this, we define another “Func” with two parameters of “double” data type for the cubic function. One parameter is for input, and the other is for the output result and then gives a name to this, which is “cubic”. In the following line, we will show the output, and for this, we use “Console.WriteLine”. Then, define the cubic function and pass the parameter to this cubic function.

After this, we have the square method, which is the power of “2”, and define this as “static double Square (double number) => Math.Pow (number, 2)”. This method is used for getting the power of a number whose data type is “double” and Math.Pow is a method for getting the power of a number entered. Also, we have another cubic method with the power of “3”. As we know, a cubic is the cube of the number, or the number has the power of “3”. Here, we have the “Math.Pow” method. This method is used to calculate the given power of a number. It calculates the cube of a number in this line.

The output of the code is shown in the image. Let’s look at this output as it returns the square and cube of the number “5”.

Conclusion:

In this tutorial, we show you how the “C# Func” works. After reading this tutorial, we know that it is a generic delegate. Here, we pass parameters to this “Func”. One parameter is for the result, and the other is for the inputs. We have demonstrated different examples to quickly understand how this “Func” is used in C# programming in Ubuntu 20.04. We have tried our best to explain every element which we are using in our codes and examples of the C# program so that you don’t face any difficulties when you are trying this code by yourself. We hope you found this article helpful.

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.