Func
Delegate In C#
Func
Delegate in C#:
·
A delegate is a type safe
mechanism, one of the important and built -in delegate or a built-in generic
delegate is “Func Delegate”.
·
When ever we define a delegate
then we should be analyzing the requirement whether it must have return type,
either one or multiple parameter based on the method defined.
·
We can say func delegate as Function
delegate which means method with return type and zero to one or more parameter.
·
Func Delegate has 17 overloaded
methods i.e. Method with no parameter to method takes 16 parameters.
·
Func Delegate has been designed
to support the method has return type.
·
Func Delegate has been designed
to work with Anonymous type as well as lambda expressions.
·
Func Delegate has been
initiated with 3.0.
Syntax:
[Access Modifier] [Func<No
Of Input parameter(Optional) + One Output Parameter(Last One)>] [Name of
Delegate]
Example,
#region Func Delegate In c#
public class FuncDelegateDemo
{
DelegateEnum _funcDelegateTypeEnum = default(DelegateEnum);
string Fname = "DotNet";
string Lname = "360World";
StringBuilder _stringBuilder = new StringBuilder();
// Here
Func Delegate has been defined without Parameter
public Func<string> FuncDelegateWithoutParameter;
// Here
Func Delegate has been defined with one Parameter
public Func<string, string>
FuncDelegateWithParameter;
public enum DelegateEnum
{
FuncwithoutParameter = 1,
FuncwithParameter = 2,
}
public FuncDelegateDemo(DelegateEnum delegateEnum,ref StringBuilder stringBuilder)
{
_funcDelegateTypeEnum = delegateEnum;
_stringBuilder = stringBuilder;
FuncDelegateWithoutParameter = MethodOne;
FuncDelegateWithParameter = MethodTwo;
callingMethod();
}
/// <summary>
/// Method With Zero Parameter
/// </summary>
/// <returns></returns>
private string MethodOne()
{
return $"{Fname + Lname} - Returned From
FuncWithoutParameter";
}
/// <summary>
/// Method with One Parameter
/// </summary>
/// <param name="methodName"></param>
/// <returns></returns>
private string MethodTwo(string methodName)
{
return $"{Fname + Lname}- Returned From {methodName}";
}
/// <summary>
/// callingMethod
/// </summary>
private void callingMethod()
{
string respone = string.Empty;
switch (_funcDelegateTypeEnum)
{
case DelegateEnum.FuncwithoutParameter:
_stringBuilder.AppendLine("==============Method Calling FUNC without Parameter Starts===============");
// Calling method one with func
Delegates with zero parameter
respone = FuncDelegateWithoutParameter();
_stringBuilder.AppendLine($"Output - {respone}");
_stringBuilder.AppendLine("==============Method Calling FUNC without Parameter Ends===============");
break;
case DelegateEnum.FuncwithParameter:
_stringBuilder.AppendLine("==============Method Calling FUNC with Parameter Starts===============");
// Calling methodTwo with func
Delegates with one parameter
respone = FuncDelegateWithParameter("FuncDelegateWithParameter");
_stringBuilder.AppendLine($"Output - {respone}");
_stringBuilder.AppendLine("==============Method Calling FUNC with Parameter Ends===============");
break;
}
}
}
/// <summary>
/// funcDelegateBtn_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void funcDelegateBtn_Click(object sender,
EventArgs e)
{
StringBuilder stringBuilder = new StringBuilder();
// Calling for without parameter - FUNC
FuncDelegateDemo funcDelegateDemo = new
FuncDelegateDemo(FuncDelegateDemo.DelegateEnum.FuncwithoutParameter, ref stringBuilder);
// Reinitialized - Calling for with parameter - FUNC
funcDelegateDemo = new
FuncDelegateDemo(FuncDelegateDemo.DelegateEnum.FuncwithParameter, ref stringBuilder);
MessageBox.Show(stringBuilder.ToString());
}
#endregion
Output
Comments
Post a Comment