Delegates let you store and use methods like variables. This helps you call different methods flexibly without changing your code.
When you want to pass a method as a parameter to another method.
When you want to decide which method to call at runtime.
When you want to implement event handling in your program.
When you want to write reusable and flexible code that can work with different methods.
When you want to create callback methods that run after some task finishes.
Syntax
C Sharp (C#)
delegate returnType DelegateName(parameterList);
A delegate defines a type that can hold references to methods with a specific signature.
You create delegate instances by assigning methods that match the delegate's signature.
Examples
Defines a delegate named SimpleDelegate that points to methods with no parameters and no return value.
C Sharp (C#)
delegate void SimpleDelegate();
Defines a delegate named MathOperation for methods that take two integers and return an integer.
C Sharp (C#)
delegate int MathOperation(int x, int y);
Sample Program
This program shows how delegates let you choose which method to call at runtime. We define a delegate MathOperation, then assign it to Add and Multiply methods. Calling the delegate runs the assigned method.
C Sharp (C#)
using System;
class Program
{
// Define a delegate that takes two ints and returns an int
delegate int MathOperation(int a, int b);
staticint Add(int x, int y) => x + y;
staticint Multiply(int x, int y) => x * y;
staticvoid Main()
{
// Create delegate instances
MathOperation op = Add;
Console.WriteLine("Add: " + op(3, 4));
op = Multiply;
Console.WriteLine("Multiply: " + op(3, 4));
}
}
OutputSuccess
Important Notes
Delegates are type-safe, meaning they only point to methods with matching signatures.
They are the foundation for events and callbacks in C#.
Summary
Delegates let you treat methods like variables for flexible code.
They help call different methods without changing the calling code.
Delegates are key for events and callbacks in C#.
Practice
(1/5)
1. Why are delegates needed in C# programming?
easy
A. To allow methods to be passed as parameters and called flexibly
B. To store data in a database
C. To create user interfaces
D. To perform arithmetic calculations
Solution
Step 1: Understand the role of delegates
Delegates let you treat methods like variables, so you can pass them around and call them flexibly.
Step 2: Compare with other options
Storing data, creating interfaces, or arithmetic are unrelated to delegates' purpose.
Final Answer:
To allow methods to be passed as parameters and called flexibly -> Option A
Quick Check:
Delegates enable flexible method calls = D [OK]
Hint: Delegates let you pass methods like variables [OK]
Common Mistakes:
Confusing delegates with data storage
Thinking delegates create UI elements
Assuming delegates perform calculations
2. Which of the following is the correct syntax to declare a delegate in C#?
easy
A. void delegate MyDelegate(string message);
B. delegate void MyDelegate(string message);
C. delegate MyDelegate void(string message);
D. void MyDelegate delegate(string message);
Solution
Step 1: Recall delegate declaration syntax
The correct syntax starts with the keyword 'delegate', followed by the return type, then the delegate name and parameters.
Step 2: Check each option
delegate void MyDelegate(string message); matches the correct syntax. Others have incorrect order or keywords.
Final Answer:
delegate void MyDelegate(string message); -> Option B
Quick Check:
Correct delegate syntax = A [OK]
Hint: Delegate syntax starts with 'delegate' keyword [OK]
Common Mistakes:
Placing 'void' before 'delegate'
Mixing order of keywords
Omitting 'delegate' keyword
3. What will be the output of the following C# code?
delegate void Printer(string message);
class Program {
static void PrintHello(string msg) {
Console.WriteLine("Hello " + msg);
}
static void Main() {
Printer p = PrintHello;
p("World");
}
}
medium
A. PrintHello World
B. World Hello
C. Hello World
D. Compilation error
Solution
Step 1: Understand delegate assignment and call
The delegate 'Printer' is assigned to method 'PrintHello', which prints "Hello " plus the message.