What are Func, Predicate, Action delegates with Examples ? #AspNetInterviewQuestionsSeries

To learn Delegate you guys can refer my previous article. In this article we will learn about what are Func, Predicate and Action Delegate.
Keeping the power of delegates in the mind .Net Framework has introduced the predefined delegates which can be useful for a developer. .Net Framework has also used these predefined delegates at many places e.g LINQ.

Action Delegate: As its name is ACTION this delegate ensures to performs actions and does not return anything. Action Delegate can take upto 16 parameters and returns nothing as they are void delegates and their primary task to perform some action. You can create Action in C# :

// define action 
Action<string> myAction;
// F12 definition will be of your action, it can go upto 16 parameters
public delegate void Action<in T>(T obj);


Example : they are most commonly getting used in List<T>.ForEach  like functions.

Func Delegate : Once again remember its by name it is a function type delegate which will  have a return type. Func Delegate’s last parameter determine its Output type.

//define func 
Func<string, string > abc;

Func can have maximum 16 input parameters and last parameter/single parameter will always be the output type o that delegate.

Example : Linq using func delegates in SELECT, WHERE , JOIN and more..

Predicate Delegate : We can say predicate is wrapper of func<T1, out Boolean> . Predicate takes only one parameter and returns Boolean as an output.

//define predicate,predicate takes only one parameter
 Predicate<string > abc;

Example :  Predicate is mostly used in List<T> for methods like FindAll and RemoveAll.

How you can write your own Action,Func,Predicate delegate ,Running Code Example:


Code In case .Net Fiddle not works :

using System;
public class Program
	public static void Main()
		// * Action Delegate *
		Action<string> PerformActionDoNotReturn = new Action<string>(WriteInConsole);
		// can be also called as PerformActionDoNotReturn.Invoke("asasasas");
		PerformActionDoNotReturn("Yeah i am anAction"); 
		// *Func Delegate*
		Func<int int="">  PerformActionReturnMeSomething = new Func<int int="">(DoubleMyMoney);
		// *Predicate Delegate*
		Predicate<object> checkStringType = new Predicate<object>(IsThisString);
	static void WriteInConsole(string str)
	static int DoubleMyMoney(int mymoney)
	  return mymoney * mymoney;
	static bool IsThisString(object str)
	   return str is string ? true : false;

Hope my this series will help somebody to get a job 🙂 🙂 . Happy coding 🙂

via Blogger http://ift.tt/2kGFozM January 29, 2017 at 03:37PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s