If at first you don't succeed, call it version 1.0

Contact Me

Sarvesh Kushwaha
Email : sarveshkushwaha@outlook.com

Total Pageviews

Powered by Blogger.

Sunday 29 January 2017

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);
		Console.WriteLine(PerformActionReturnMeSomething(10000));
		
		// *Predicate Delegate*
		Predicate<object> checkStringType = new Predicate<object>(IsThisString);
		Console.WriteLine(checkStringType(123));
		Console.WriteLine(checkStringType("fddfd"));
		
		
	}
	
	static void WriteInConsole(string str)
	{
	  Console.WriteLine(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 :)

3 comments: