delegate in c# with example

What are Generic Delegates, Types and Examples in C#?

Microsoft provides three ready-made generics delegate. This is call shorter and sweeter delegate. This to minimize the code complexity of the apps.
1.      Func< input, output >
2.      Action< inputParameter >
3.      Predicate< inputParameter >

Func <input, output > Delegate:-

The Func <input, output> generic delegate is use, when you need to some input parameter and then return some output.

The example for Func generic delegate as given below,
namespace GenericDelegate
{
    public class Delegates
    {
        static void Main(string[] args)
        {
            // int is a input parameter and double is output parameter
            Func<intdouble> calPiR2Obj = r => 3.12 * r * r;
            Console.Write(calPiR2Obj(4));
            Console.ReadLine();
        }
    }
}


Action< inputParameter > Delegate:-

The Action< inputParameter> generics delegate is use, when you need to input parameter and no need to return output that means return void.

The example for Action generics delegate as below,
namespace GenericDelegate
{
    public class Delegates
    {
        static void Main(string[] args)
        {
            Action<string> ActionObject = x => Console.WriteLine(x);
            ActionObject("This is Action delegate!");

            Console.ReadLine();
        }
    }
}


Predicate < inputParameter > Delegate:-

The Predicate generics delegate is use, when you need to input parameter and return output.
The return output is Boolean type just like true or false. This delegate is uses when you need to check the conditional statements are true or false. 

The example for Predicate delegate as below,
namespace GenericDelegate
{
    public class Delegates
    {
        static void Main(string[] args)
        {
            // string is input but out put is retun True or False
            Predicate<string> checkConditionIsTF = x => x.Length > 10;
            Console.WriteLine(checkConditionIsTF("Anil Kumar Singh"));
            Console.ReadLine();
        }
    }
}


The example for Generic Delegates:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericDelegate
{   
    public class Delegates
    {
        static void Main(string[] args)
        {
            //Int is an input parameter and double is output parameter.
            Func<intdouble > calPiR2Obj = r => 3.12 * r * r;
            Console.WriteLine(calPiR2Obj(4));

            //String is input but no output return.
            Action<string > ActionObject = x => Console.WriteLine(x);
            ActionObject("This is Action delegate!");

            //String is input but output, the return is true or false
            Predicatestring > checkConditionIsTF = x => x.Length > 10;
            Console.WriteLine(checkConditionIsTF("Anil Kumar Singh"));

            Console.ReadLine();
        }
    }
}


I hope it is very useful to you! Thank you!
ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.