Jan 23, 2020

ACTION DELEGATE IN C#(GENERIC DELEGATE p-2)

action delegate, c# action,c# lambda, lambda syntax, action delegate example, example of action delegate in c#, c# delegate
action delegate in c#


ACTION DELEGATE IN C#

           "Action delegate is one of the generic delegate it is used, if all that you have a method that return nothing for this method we use action delegate the method should be void in return type. ‘Action’ is the keyword. Simply it is used for non-value returning methods. In Func delegate we have parameter passing along with return values but in action delegate we can any parameter but we can’t have any return value. Here we have 16 overloaded delegates all have different parameter number but zero returning values which explain in table
Sr.no
Delegate
Parameter list
Return value number
1
1st delegate
1 parameter
Zero return type
2
2nd delegate
2 parameters
Zero return type
3
3rd delegate
3 parameters
Zero return type
4
4th delegate
4 parameters
Zero return type
5
5th delegate
5 parameters
Zero return type
6
6th delegate
6 parameters
Zero return type
7
7th delegate
7 parameters
Zero return type
8
8th delegate
8 parameters
Zero return type
9
9th delegate
9 parameters
Zero return type
10
10th delegate
10 parameters
Zero return type
11
11th delegate
11 parameters
Zero return type
12
12th delegate
12 parameters
Zero return type
13
13th delegate
13 parameters
Zero return type
14
14th delegate
14 parameters
Zero return type
15
15th delegate
15 parameters
Zero return type
16
16th delegate
16 parameters
Zero return type

Syntax
Action <parameter list > Obj = <method name>
Here we no need to explain the return type because it will be non-value return method. Action is the keyword parameter list will contain the passing parameter that can be any nature data type Obj will object name by which we call the delegate by passing the parameter and finally the method name replaced by that method name which will use to bind with this delegate.
It can be short if we use lambda expression on Action delegate not only Action all the three generic delegate can make easy by using lambda expression how can we make it just remove the method name and specify the lambda expression  as we did in lambda expression part like this.
Action <formal parameter type only > Obj = (actual parameter list only name) =>
{
            Method implementation;
}
you can see what is formal parameter and actual parameter by clicking on them.


EXAMPLE OF ACTION DELEGATE IN C#

            Consider this simple program we have two example 1st example method using delegate to execute and in 2nd example we use action delegate along with lambda expression to execute the program.
1st Example without ACTION delegate use
namespace ActionDelegate
{
    public delegate void Addnum(int a, double b, float c);
    public delegate void SayDelegate(string str);
    class Program
    {
        public static void Add(int a, double b, float c)
        {
            Console.WriteLine(a + b + c);
        }
        public static void Saything(string str)
        {
            Console.WriteLine ("hy " + str + " you are using Visual stdio 2020");
        }
        static void Main(string[] args)
        {
            Addnum ad = Add;
            ad(12, 23.45, 34.4f);
            SayDelegate sd = Saything;
            sd("kashif");
            Console.ReadKey();
        }
    }
}


2nd example Action delegate use
namespace ActionDelegate
{
    class Program
    {
      
        static void Main(string[] args)
        {
          Func< int , double , float > ad = (a,b,c) =>
            {
                Console.WriteLine(a + b + c);

            };
            ad(12, 23.45, 34.4f);
            Func<string > sd = (str) =>
             {
                 Console.WriteLine ("hy " + str + " you are using Visual stdio 2020");                           
             };
            sd("kashif");
            Console.ReadKey();
        }
    }
}

Predicate delegate
Func delegate

0 comments:

Post a Comment

Please do not enter any spam link in the comment box