In C# programming language, there are various delegates to use. C# provides 2 built-in delegates: Action and Func.
This article will cover Action and Func delegates in C#. Before we proceed further, we will first look into delegate.
What is a Delegate?
A delegate is an object that holds a reference to a method to execute them. We can use a delegate to implement the call-back method and event. The major advantage of using a delegate is passing the method as a parameter instead of defining a method separately.
Let’s look at the syntax to declare a delegate:
public delegate int MyDelegate(string message);
Here, we define a delegate, MyDelegate, which accepts a string parameter and returns an int.
Let’s see how to invoke a delegate in C# with an example:
public class NumberAddition
{
public delegate int AddNumDelegate(int number1, int number2);
public int AddWithDelegate(int num1, int num2)
{
return num1 + num2;
}
}
public class Program
{
static void Main(string[] args)
{
var numberAddition = new NumberAddition();
var addNumDelegate = new AddNumDelegate(numberAddition.AddWithDelegate);
int result = addNumDelegate(10, 20);
Console.WriteLine("Using Delegate | Result is {0}", result);
Console.ReadLine();
}
}
Here, we create a delegate AddNumDelegate
that accepts 2 numbers and sums them up. In the Main()
method, we create an object of the NumberAddition
class, and then we instantiate the delegate with the new keyword. Finally, we pass the method AddWithDelegate()
to our delegate as a parameter. So, in this way, the addNumDelegate
holds the reference to the method AddWithDelegate()
.
What is an Action Delegate in C#?
The Action delegate in C# is a predefined delegate that accepts 1 or more input parameters and doesn’t return a value. We use Action delegate
with a method that has a void return type.
So, we can use an Action delegate to replace a method which doesn’t return a value. We have the option to use Action Delegate with the lambda expression and anonymous method.
Let’s see how to invoke the Action delegate in C#:
public class NumberAddition
{
public static void AddWithActionDelegate(int number1, int number2)
{
Console.WriteLine("Using Action Delegate | Result is {0}", number1 + number2);
Console.ReadLine();
}
}
public class Program
{
static void Main(string[] args)
{
Action<int, int> addNumberActionDelegate = new Action<int, int>(NumberAddition.AddWithActionDelegate);
addNumberActionDelegate(10, 20);
}
}
Here, we initialize the Action delegate addNumberActionDelegate
and this delegate holds the reference to the method AddWithActionDelegate()
which is a member of NumberAddition
class.
In C#, there are 2 ways to initialize a delegate. So, we can initialize the Action delegate directly or by using a new keyword:
Action<int> printValueActionDel = AddWithActionDelegate;
//or
Action<int> printValueActionDel = new Action<int>(AddWithActionDelegate);
What is a Func Delegate in C#?
Func delegate in C# is also a predefined delegate. But, it accepts zero or more parameters. Another difference between Action and Func delegate is that Action delegate doesn’t return a value, but Func delegate does return a value.
Let’s understand the concept of Func delegate with the help of C# code:
public class NumberAddition
{
public static int AddWithFuncDelegate(int number1, int number2)
{
return number1 + number2;
}
}
public class Program
{
static void Main(string[] args)
{
Func<int, int, int> addNumberFuncDelegate = NumberAddition.AddWithFuncDelegate;
int resultWithFuncDelegate = addNumberFuncDelegate(30, 50);
Console.WriteLine("Using Func Delegate | Result is {0}", resultWithFuncDelegate);
Console.ReadLine();
}
}
Here, we define the NumberAddition
class with a method AddWithFuncDelegate()
that accepts 2 parameters. To instantiate the Func delegate, we provide one another parameter Func which works as an out parameter.
Similar to Action delegate, we can initialize Func delegate using the new keyword as well:
Func<string> printValFuncDelegate = GetValue;
//or
Func<string> printValFuncDelegate = new Func<string>(GetValue);
Conclusion
So, in this article, we have learned about Action and Func delegates in C#.
Action delegate encapsulates a method with no return type and takes a minimum of 1 parameter. On the other hand, Func delegate may accept a minimum of zero parameters. Func delegate must return a value. Action and Func may accept parameters of the same type or different types.
Keep Following: SharePointCafe.NET