A delegate is used as a method pointer in C# program. Programmers can create their own delegates according to their need, but there are three types of built-in delegates which can be be used by programmers to save their time. These three types of delegates are as follows-
- Func Delegate
- Action Action Delegate
- Predicate Delegate
Func Delegate points to a method which has a return type.
The Action delegate points to a method which does not have a return type.
The Predicate delegate points to a method which has a Boolean return type.
Function is a method that returns a value, on the contrary, subroutine or action is a method which does not return anything. In other words, a function that does not return a value is called action. So, The Action delegate points to the subroutine or method containing the action. Predicate delegate is actually a special type of Func delegate in which the return type of the method is Boolean.
Let us now understand the concept of delegate with the help of an example.
First let's create a method Add which returns double type data and then call it by using the Func delegate.
public double Add(double number1, double number2)
{
return number1 + number2;
}
Now we create a method Product which returns void and then call it by using the the Action delegate.
public void Product(double number1, double number2)
{
double product = number1 * number2;
Console.WriteLine("Action Delegate: Product is " + product.ToString());
}
Now we create a method Status which returns Boolean type data and then call it by using the Predicate delegate.
public bool Status(int age)
{
if (age >= 18)
return true;
return false;
}
Depending on the return type of the methods, we call the methods with the help of different types of delegate.
using System;
namespace BuiltInDelegates
{
class Program
{
public double Add(double number1, double number2)
{
return number1 + number2;
}
public void Product(double number1, double number2)
{
double product = number1 * number2;
Console.WriteLine("Action Delegate: Product is " + product.ToString());
}
public bool Status(int age)
{
if (age >= 18)
return true;
return false;
}
static void Main(string[] args)
{
Program obj = new Program();
Func AddDelegateObj = obj.Add;
double result = AddDelegateObj.Invoke(22.5, 44.6);
Console.WriteLine("Func Delegate: Sum is "+result.ToString());
Action ProductDel = obj.Product;
ProductDel(2, 4);
Predicate PredicateDel = obj.Status;
if (PredicateDel(17))
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
Console.ReadKey();
}
}
}
Func<double, double, double> In this, the first two data types indicate the data type of the input parameters of the called method while the last parameter indicates the return type of the called method.
Action<double, double> In this, both data types indicate the input parameters of the called method because the action delegate refers to a method that does not return anything.
Predicate<int> Indicates that the called method has only one input parameter and that the return type of that method is Boolean data type
In the case of func only, the last parameter indicates the return type of the method, while in both the cases, the parameters indicate only the input parameters of the called method.
The above mentioned three methods can also be converted as anonymous methods and then call them with the help of generic delegate like Function, Action or Predicate. It is presented by the following example.
using System;
namespace BuiltInDelegates
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
Func AddDelegateObj = delegate (double number1, double number2)
{
return number1 + number2;
};
double result = AddDelegateObj.Invoke(22.5, 44.6);
Console.WriteLine("Func Delegate: Sum is "+result.ToString());
Action ProductDel = delegate (double number1, double number2)
{
double product = number1 * number2;
Console.WriteLine("Action Delegate: Product is " + product.ToString());
};
ProductDel(2, 4);
Predicate PredicateDel = delegate (int age)
{
if (age >= 18)
return true;
return false;
};
if (PredicateDel(17))
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
Console.ReadKey();
}
}
}
LAMBDA EXPRESSION
using System;
namespace BuiltInDelegates
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
Func AddDelegateObj = (double number1, double number2) =>
{
return number1 + number2;
};
double result = AddDelegateObj.Invoke(22.5, 44.6);
Console.WriteLine("Func Delegate: Sum is "+result.ToString());
Action ProductDel = (double number1, double number2) =>
{
double product = number1 * number2;
Console.WriteLine("Action Delegate: Product is " + product.ToString());
};
ProductDel(2, 4);
Predicate PredicateDel = (int age) =>
{
if (age >= 18)
return true;
return false;
};
if (PredicateDel(17))
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
Console.ReadKey();
}
}
}
No comments:
Post a Comment