. Advertisement .
..3..
. Advertisement .
..4..
In some programming languages like C++, you can use pointers to refer to functions. This isn’t applicable to C#. Read on to find out how to pass a function as a parameter in C#.
Pass A Function As A Parameter In C#
Delegates In C#
To pass a function to another function in C#, you will need to use a delegate. This object presents a reference to a method, including a return type and a parameter list.
After creating a delegate, you can use it with any method that has a compatible return type and signature. This method can then be called or invoked through that delegate instance.
Delegates can be used to pass methods (like functions) as arguments to other methods in C#. A function from an accessible struct or class that matches its type can be assigned to a delegate. This function can be an instance or a static function.
With this flexibility, you can insert new code into an existing class or programmatically change function calls. Since they can refer to a function as a parameter, delegates in C# are an ideal way to define callback functions.
Here are some important properties of C# delegates:
- They share many similar aspects with function points in C++. However, C# delegates are entirely object-oriented, secure, and safe. They can also encapsulate both a method and an object instance, unlike pointers to functions in C++.
- You can pass methods (like functions) as parameters with them.
- You can use them for defining callback methods.
- You can chain them together to, for instance, call multiple methods on one event.
- The type of delegate doesn’t exactly have to match methods.
- In some contexts, C# compiles lambda expressions into delegate types.
- You can manually construct a delegate in C# with a lambda expression or by using the name of the function it needs to wrap. However, C# comes with two delegates in the System namespace you can easily use to pass a function as a parameter: Func and Action.
Func Delegate
The Func delegate can wrap a function that has only one parameter. It will return a type specified by its constructor. This is the syntax of defining a Func delegate:
public delegate TResult Func<in T,out TResult>(T arg);
In which:
- T: the type of the sole parameter of the function that this Func delegate needs to encapsulate. This parameter is contravariant, meaning not only can you use the type it specifies, but a type less derived can also be used.
- TResult: the type of the return value of the function that this FUnc delegate needs to encapsulate. This parameter is covariant, meaning not only can you use the type it specifies, but a more derived type can also be used.
- arg: the parameter of the function this Func delegate needs to encapsulate.
- TResult: the return value of the function you need to pass as a function.
namespace ITTutoria
{
class Program
{
static void Main()
{
Console.WriteLine("Enter your number:");
int n = Convert.ToInt32(Console.ReadLine());
secondFunction(firstFunction, n);
}
static int firstFunction(int number)
{
return number * number;
}
static void secondFunction(Func<int, int> function, int number)
{
var result = function(number);
Console.WriteLine(result);
}
}
}
Output:
Enter your number:
9
81
Action Delegate
When you need to pass a function that has only one parameter but no return value, use the Action delegate. Its constructor:
public delegate void Action<in T>(T obj);
- T: the type of the parameter of the function you need to pass as a parameter.
- obj: the parameter of the function you need to pass as a parameter.
Example:
List<string> sites = new List<string>();
sites.Add("ITTutoria");
sites.Add("Stack Overflow");
sites.Add("Quora");
sites.Add("Reddit");
sites.ForEach(Print);
sites.ForEach(delegate(string name)
{
Console.WriteLine(name);
});
void Print(string s)
{
Console.WriteLine(s);
}
Output:
ITTutoria
Stack Overflow
Quora
Reddit
ITTutoria
Stack Overflow
Quora
Reddit
Conclusion
To pass a function as a parameter in C#, you will need to use a delegate like Func or Action. It encapsulates your function and refers to it with a particular return type and parameter list.
Leave a comment