Write a C# Program to Call Math Operations using Delegates
This C# Program Calls Math Operations using Delegates. Here the Delegates are used to encapsulate functions into callable function objects that are used much as function pointers are used in C, C++, and other languages. Like function pointers, delegates enable you to separate a function reference from its implementation, allowing the implementation to exist in a separate module.
Here is source code of the C# Program to Call Math Operations using Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
ANS:
using System;
public class MathOperations
{
public static double Multiply(double value)
{
return value * 2;
}
public static double Square(double value)
{
return value * value;
}
}
delegate double DoubleOp(double x);
class Application
{
static void Main()
{
DoubleOp[] operations =
{
MathOperations.Multiply,
MathOperations.Square
};
for (int i = 0; i < operations.Length; i++)
{
Console.WriteLine("Operation[{0}]:", i);
ProcessAndDisplayNumber(operations[i], 5.0);
ProcessAndDisplayNumber(operations[i], 13.55);
ProcessAndDisplayNumber(operations[i], 1.732);
Console.WriteLine();
}
Console.ReadLine();
}
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
double result = action(value);
Console.WriteLine(
"Value : {0} Result : {1}", value, result);
}
}
Output:
Operation[0]:
Value : 5 Result : 10
Value : 13.55 Result : 27.1
Value : 1.732 Result : 3.464
Operation[1]:
Value : 5 Result : 25
Value : 13.55 Result : 183.6025
Value : 1.732 Result : 2.999824
This C# Program Calls Math Operations using Delegates. Here the Delegates are used to encapsulate functions into callable function objects that are used much as function pointers are used in C, C++, and other languages. Like function pointers, delegates enable you to separate a function reference from its implementation, allowing the implementation to exist in a separate module.
Here is source code of the C# Program to Call Math Operations using Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
ANS:
using System;
public class MathOperations
{
public static double Multiply(double value)
{
return value * 2;
}
public static double Square(double value)
{
return value * value;
}
}
delegate double DoubleOp(double x);
class Application
{
static void Main()
{
DoubleOp[] operations =
{
MathOperations.Multiply,
MathOperations.Square
};
for (int i = 0; i < operations.Length; i++)
{
Console.WriteLine("Operation[{0}]:", i);
ProcessAndDisplayNumber(operations[i], 5.0);
ProcessAndDisplayNumber(operations[i], 13.55);
ProcessAndDisplayNumber(operations[i], 1.732);
Console.WriteLine();
}
Console.ReadLine();
}
static void ProcessAndDisplayNumber(DoubleOp action, double value)
{
double result = action(value);
Console.WriteLine(
"Value : {0} Result : {1}", value, result);
}
}
Output:
Operation[0]:
Value : 5 Result : 10
Value : 13.55 Result : 27.1
Value : 1.732 Result : 3.464
Operation[1]:
Value : 5 Result : 25
Value : 13.55 Result : 183.6025
Value : 1.732 Result : 2.999824
No comments:
Write comments