In this post we are going to see Action and Func. Action will have a reference of method with out return type, function have return type as last parameter.
Output :-
*** Action sample ***
Read Data : Started
Write Data : Started
*** Func Sample ***
Sample
Employee Rajesh
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
/* Action Sample */
Console.WriteLine("***
Action sample ***");
Action read = ReadData;
Action write = WriteData;
ExecuteActions(new List<Action>()
{ read,write});
Console.WriteLine();
Console.WriteLine("***
Func Sample ***");
/* Function samples */
Func<string>
returnmethod = Sam;
Func<string, string> empfunc = Name;
Console.WriteLine(returnmethod.Invoke());
Console.WriteLine(empfunc("Rajesh"));
Console.Read();
}
public static string Sam()
{
return "Sample";
}
public static string Name(string
name)
{
return "Employee
" + name;
}
public static void ExecuteActions(List<Action> actions)
{
foreach (Action
act in actions)
{
act.Invoke();
}
}
public static void ReadData()
{
Console.WriteLine("Read
Data : Started ");
}
public static void WriteData()
{
Console.WriteLine("Write
Data : Started");
}
}
}
*** Action sample ***
Read Data : Started
Write Data : Started
*** Func Sample ***
Sample
Employee Rajesh
From this article you can learn how to use action and func
No comments:
Post a Comment