3
Answers

Delegate program and Modification

Maha

Maha

9y
546
1

This program is a given delegate program. Methods in the class are static. 2nd program is the same one but I made the method non-static. How to complete the program if the methods in the class are non-static.

1st program

using System;

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

    public static double Square(double value)
    {
        return value * value;
    }
}
//We call up these methods like this:

delegate double DoubleOp(double x);

class Application
{
    static void Main()
    {
        DoubleOp[] operations =
            {
               MathOperations.MultiplyByTwo,
               MathOperations.Square
            };

        for (int i = 0; i < operations.Length; i++)
        {
            Console.WriteLine("Using operations[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 2.0);
            ProcessAndDisplayNumber(operations[i], 7.94);
            ProcessAndDisplayNumber(operations[i], 1.414);
            Console.WriteLine();
        }
        Console.ReadKey();
    }

    static void ProcessAndDisplayNumber(DoubleOp action, double value)
    {
        double result = action(value);
        Console.WriteLine(
           "Value is {0}, result of operation is {1}", value, result);
    }
}
/*
Using operations[0]:
Value is 2, result of operation is 4
Value is 7.94, result of operation is 15.88
Value is 1.414, result of operation is 2.828

Using operations[1]:
Value is 2, result of operation is 4
Value is 7.94, result of operation is 63.0436
Value is 1.414, result of operation is 1.999396
*/

2nd program

using System;

public class MathOperations
{
    public double MultiplyByTwo(double value) //Made non-static method
    {
        return value * 2;
    }

    public double Square(double value) //Made non-static method
    {
        return value * value;
    }
}
//We call up these methods like this:

delegate double DoubleOp(double x);

class Application
{
    static void Main()
    {
        MathOperations mo = new MathOperations();
        DoubleOp doubleOp1 = new DoubleOp(mo.MultiplyByTwo);
        DoubleOp doubleOp2 = new DoubleOp(mo.Square);

        DoubleOp[] operations =
            {
               doubleOp1.MultiplyByTwo,
               doubleOp2.Square
            };

        for (int i = 0; i < operations.Length; i++)
        {
            Console.WriteLine("Using operations[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 2);
            ProcessAndDisplayNumber(operations[i], 7.94);
            ProcessAndDisplayNumber(operations[i], 1.414);
            Console.WriteLine();
        }
        Console.ReadKey();
    }

    static void ProcessAndDisplayNumber(DoubleOp action, double value)
    {
        double result = action.Invoke(value);
        Console.WriteLine(
           "Value is {0}, result of operation is {1}", value, result);
    }
}

 

Answers (3)