C# Func and Action Type Example
From this article I'm going to show simple example of Func and Action Type in C#. before go to the coding let's see what's Func and what is Action. so in the previous article we talked about the delegates. so these func and Action types are also derived from delegate type.
1. Func
Func handle method with parameters therefore we say Func is Parameterized type. in Func we specified number and kind of parameters and what should be the return type. And Func type provides a way to store anonymous methods in generalized and simple way. you can find more information about Func here.
2. Action
Action object return no value. so this is similar to void method in C# and we can use Action type to handle Void methods. Action method can found in System namespace. You can find more information about Action here.
so let's go to a sample code and try to get idea about these concepts.
So to call AddTwoValue Method I used Func Type variable so AddTwoMethod expect two intger parameters and returns integer value. so the 1st two parameters in the calculationhandler variable are input parameters and 3rd item is for the return type.
As I explained before Action doesn't return any value but that also can expect input parameters. so DisplayMethod variable expect one string input parameter and return nothing. so both method are derived from the delegate method and kind of easy than the delegate.
so here is the output of the following code segment. see it behave just like normal approach.
1. Func
Func handle method with parameters therefore we say Func is Parameterized type. in Func we specified number and kind of parameters and what should be the return type. And Func type provides a way to store anonymous methods in generalized and simple way. you can find more information about Func here.
2. Action
Action object return no value. so this is similar to void method in C# and we can use Action type to handle Void methods. Action method can found in System namespace. You can find more information about Action here.
so let's go to a sample code and try to get idea about these concepts.
class Program
{
/// <summary>
/// The value1
/// </summary>
const int Value1 = 3;
/// <summary>
/// The value2
/// </summary>
const int Value2 = 4;
/// <summary>
/// The name
/// </summary>
private const string Name = "John";
/// <summary>
/// Mains the specified arguments.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
Func<int,int, int> calculationHandeler = AddTwoValues;
int result = calculationHandeler(Value1, Value2);
Console.WriteLine(result);
Action<string> displayMethod = ShowName;
displayMethod(Name);
Console.ReadLine();
}
/// <summary>
/// Adds two values.
/// </summary>
/// <param name="value1">The value1.</param>
/// <param name="value2">The value2.</param>
/// <returns></returns>
public static int AddTwoValues(int value1, int value2)
{
return value1 + value2;
}
/// <summary>
/// Shows name.
/// </summary>
/// <param name="name">The name.</param>
public static void ShowName(string name)
{
Console.WriteLine(string.Format("Hello {0}",name));
}
}
So to call AddTwoValue Method I used Func Type variable so AddTwoMethod expect two intger parameters and returns integer value. so the 1st two parameters in the calculationhandler variable are input parameters and 3rd item is for the return type.
As I explained before Action doesn't return any value but that also can expect input parameters. so DisplayMethod variable expect one string input parameter and return nothing. so both method are derived from the delegate method and kind of easy than the delegate.
so here is the output of the following code segment. see it behave just like normal approach.
Comments
Post a Comment