Thursday 18 October 2018

Delegate, Func, Action, Predicate

Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. Delegate is like function pointer in C++.
To read more delegates-c-sharp.html

Different Flavors of Delegate
  1. Func<TParameter, TOutput>
  2. Action<TParameter>
  3. Predicate<in T>
1- Func is logically similar to base delegate implementation. The difference is in the way we declare. At the time of declaration, we need to provide the signature parameter & its return type.

Func<string, int, int> tempFuncPointer;

Func is always used when you have return object or type from method. If you have void method, you should be using Action.

2- Action is used when we do not have any return type from method. Method with void signature is being used with Action delegate.

Action<string, int> tempActionPointer;

Similar to Func delegate, the first two parameters are the method input parameters. Since we do not have return object or type, all the parameters are considered as input parameters.

3- Predicate is a function pointer for method which returns boolean value. It is used to represent a set of criteria and determine if the argument matches the criteria. Declaration for the same looks like this:

Predicate<Employee> tempPredicatePointer;

Example Code: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAppConsole
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Action<int> myAction = new Action<int>(DoSomething);
            myAction(123);           // Prints out "123"
                                     // can be also called as myAction.Invoke(123);

            Func<int, double> myFunc = new Func<int, double>(CalculateSomething);
            Console.WriteLine(myFunc(5));   // Prints out "2.5"


            Predicate<int> myPred = new Predicate<int>(CheckVal);   
            Console.WriteLine(myPred(1));    // Prints out "true"
           
            Console.ReadLine();
        }

        static void DoSomething(int i)
        {
            Console.WriteLine(i);
        }

        static double CalculateSomething(int i)
        {
            return (double)i / 2;
        }

        static bool CheckVal(int i)
        {
            return true;
        }

    }
}

In short:
Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
Predicate is a special kind of Func often used for comparisons.

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives