Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to call Math Operations using Delegates in C#?
To understand how to call Math Operations using Delegates in C#, let us see an example wherein we will perform mathematical operations like division, multiplication, and addition using delegates. A delegate in C# is a type that represents references to methods with a specific signature.
Syntax
Following is the syntax for declaring a delegate that can reference mathematical methods −
delegate returnType DelegateName(parameters);
Following is the syntax for assigning methods to a delegate −
DelegateName delegateInstance = MethodName; // or DelegateName delegateInstance = new DelegateName(MethodName);
Using Delegates for Basic Math Operations
Let's create a comprehensive example that demonstrates various math operations using delegates −
using System;
public class MathOperations {
public static double Add(double a, double b) {
return a + b;
}
public static double Subtract(double a, double b) {
return a - b;
}
public static double Multiply(double a, double b) {
return a * b;
}
public static double Divide(double a, double b) {
return a / b;
}
}
delegate double MathDelegate(double x, double y);
class Program {
public static void Main() {
MathDelegate add = MathOperations.Add;
MathDelegate subtract = MathOperations.Subtract;
MathDelegate multiply = MathOperations.Multiply;
MathDelegate divide = MathOperations.Divide;
double num1 = 20, num2 = 5;
Console.WriteLine("Addition: " + add(num1, num2));
Console.WriteLine("Subtraction: " + subtract(num1, num2));
Console.WriteLine("Multiplication: " + multiply(num1, num2));
Console.WriteLine("Division: " + divide(num1, num2));
}
}
The output of the above code is −
Addition: 25 Subtraction: 15 Multiplication: 100 Division: 4
Using Delegate Arrays for Multiple Operations
You can store multiple delegates in an array and call them sequentially −
using System;
public class Demo {
public static double DivideFunc(double value) {
return value / 5;
}
public static double MultiplyFunc(double value) {
return value * 3;
}
public static double SquareFunc(double value) {
return value * value;
}
}
delegate double myDelegate(double x);
class Program {
static void Main() {
myDelegate[] operations = {
Demo.DivideFunc,
Demo.MultiplyFunc,
Demo.SquareFunc
};
double inputValue = 10;
for (int i = 0; i < operations.Length; i++) {
ExecuteOperation(operations[i], inputValue, i + 1);
}
}
static void ExecuteOperation(myDelegate d, double value, int opNumber) {
double result = d(value);
Console.WriteLine("Operation {0} Result = {1}", opNumber, result);
}
}
The output of the above code is −
Operation 1 Result = 2 Operation 2 Result = 30 Operation 3 Result = 100
Multicast Delegates for Math Operations
Delegates can be combined using the + operator to create multicast delegates that call multiple methods −
using System;
public class Calculator {
public static void ShowSquare(double num) {
Console.WriteLine("Square of {0} = {1}", num, num * num);
}
public static void ShowCube(double num) {
Console.WriteLine("Cube of {0} = {1}", num, num * num * num);
}
public static void ShowDouble(double num) {
Console.WriteLine("Double of {0} = {1}", num, num * 2);
}
}
delegate void CalculationDelegate(double x);
class Program {
public static void Main() {
CalculationDelegate calc = Calculator.ShowSquare;
calc += Calculator.ShowCube;
calc += Calculator.ShowDouble;
Console.WriteLine("Performing multiple calculations for number 4:");
calc(4);
}
}
The output of the above code is −
Performing multiple calculations for number 4: Square of 4 = 16 Cube of 4 = 64 Double of 4 = 8
Conclusion
Delegates in C# provide a powerful way to call mathematical operations dynamically. They allow you to store method references, create arrays of operations, and even combine multiple methods using multicast delegates. This makes your code more flexible and enables scenarios like callback methods and event handling in mathematical computations.
