declaring a delegate array

We can also declare a delegate array. However, each element of the delegate array must be of same signature in terms of input arguments and return types.

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

namespace ConsoleApplication10
{
    class Program
    {
        delegate double DoubleOp(double x);

        static void Main(string[] args)
        {
            /*
            DoubleOp[] operations = new DoubleOp[2];

            operations[0] = Program.MultiplyByTwo;
            operations[1] = Program.getSquare;

            Console.WriteLine( operations[0](10) );
            Console.WriteLine(operations[1](20));
            */

 DoubleOp[] operations = { Program.MultiplyByTwo, Program.getSquare };

            foreach(DoubleOp op in operations)
            {
                Console.WriteLine( op(10) );  
            }

        }

        public static double MultiplyByTwo(double value)
        {
            return value * 2;
        }

        public static double getSquare(double value)
        {
            return value*value;
        }
        
    }
}

 

Tags:,

Add a Comment

Your email address will not be published. Required fields are marked *