Generic Delegates
in C#
"Like generic classes and generic methods,
a delegate can define its own type parameters"
Following example explained Generic
delegate with type parameters. I. e delegate T SampleMatOperations<T>
Generic Delegate in
C# with Real-Time Example
//Delegate declaration
public delegate T SampleMathOperations<T>(T x, T y);
//Math operations class
public class MathOperations
{
public int Addition(int x,int y)
{
return x + y;
}
public int Subtract(int a,int b)
{
return a - b;
}
}
Create instance and invoke
generic delegate
class Program
{
static void
Main(string[] args)
{
int a = 50;int b = 75;
var matOpObject = new MathOperations();
//Generic
delegate instance
//additions
SampleMathOperations<int> mathOperations = new SampleMathOperations<int>(matOpObject.Addition);
//invoke
Console.WriteLine("Add result on two
numbers"
+ mathOperations(a, b));
//subtraction
(multicast delegate)
mathOperations +=
matOpObject.Subtract;
//invoke
Console.WriteLine("Sub result on two numbers"
+ mathOperations(a, b));
}
}
Inbuilt generic delegates
Microsoft provided three inbuilt
generic delegates delegates introduced in C# 3 “System namespace”, those are Func,
Action, and Predicate.
All these three delegates can be used
with the method, anonymous method, and lambda expressions.
Func generic delegate
in C#
When to use?
“Whenever delegates return some value with
optional input parameters. i.e C# method with return value”
Description
This delegate takes one or more input
parameters and returns one out parameter. The last parameter is considered as
the return value.
It can take up to 16 input parameters
of different types and those are optional, it must have one return type that is
mandatory.
Action generic delegate
in C#
When to use?
“Whenever delegates do not return any
value with optional input parameters. i.e. C# method with void return value”
Description
It takes one or more input parameters
and returns nothing.
This delegate can take a maximum of 16
input parameters of the different or same type.
Predicate Generic
Delegate in C#
When to use?
“Whenever delegates return a Boolean
value with optional input parameters. i.e. C# method with Boolean return value”
Description
It takes one input parameter and
always returns a Boolean value which is mandatory.
This delegate can take a maximum of 1
input parameter and always return the value of the Boolean type.
Inbuilt Generic
Delegates in C# with Real-Time Examples
Sample C# class
with methods
public class GenericDelegatesSample
{
public int Addition(int x, int y)
{
return x + y;
}
public bool IsValidNumber(int x)
{
return x > 0 ? true : false;
}
public void DisplayResult(int x,int y)
{
var result = Addition(x, y);
Console.WriteLine("Addition is :" + result);
}
}
Create instance of Inbuilt Generic
delegates and invoke
class Program
{
static void Main(string[] args)
{
int a = 50;int b = 75;
var opObject = new GenericDelegatesSample();
//Func generic delegate
Func<int, int, int> add = opObject.Addition;
//invoke
Console.WriteLine("Display result :" + add(a,b));
//Action generic delegate
Action<int, int> displayResult = opObject.DisplayResult;
//invoke
displayResult(a, b);
//Predicate generic delegate
Predicate<int> isValidNum = opObject.IsValidNumber;
//invoke
Console.WriteLine("is valid number:" + isValidNum(a));
}
}