Can explain Delegates in Dotnet Framework
Loading
Can explain Delegates in Dotnet Framework
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
RinkiRank #188 · 12.9K reputationPosted Jun 2, 2025, 2:34 AM EDT
Delegates are a type-safe way to define and use function pointers. They are especially useful for event handling and callback methods.
You can read more here - https://www.c-sharpcorner.com/article/what-is-delegates-in-c-sharp-net/
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 27, 2025, 1:28 AM EDT
What is a Delegate in .NET Framework?
A Delegate in C# is like a pointer to a function — it holds a reference to one or more methods with a specific signature and return type.
In simple words: A delegate is a type-safe function pointer that allows you to call methods indirectly.
Why Use Delegates?
Delegates are mainly used for:
Event handling (they are the base of Events)
Callback methods (methods passed as parameters)
Flexible code design (loose coupling between classes)
Syntax Example
using System; public class Program { // Step 1: Declare a delegate public delegate void MyDelegate(string message); // Step 2: Create a method that matches the delegate signature public static void ShowMessage(string msg) { Console.WriteLine("Message: " + msg); } public static void Main() { // Step 3: Create delegate instance and assign method MyDelegate del = ShowMessage; // Step 4: Invoke delegate del("Hello Sandhiya! This is a delegate example."); } }Output:
How It Works
MyDelegatedefines the method signature (void, string).ShowMessagematches that signature.delstores a reference toShowMessage.When
del()is called ? it runsShowMessage().Types of Delegates
Multicast Example
public delegate void MyDelegate(); static void Hello() => Console.WriteLine("Hello!"); static void Welcome() => Console.WriteLine("Welcome!"); MyDelegate del = Hello; del += Welcome; del(); // Invokes both methodsOutput:
Common Built-in Delegates
Key Points
Delegates are type-safe (method signature must match).
They enable loose coupling between sender and receiver.
Used extensively in events, LINQ, and async programming.
In Short
Delegate = A variable that holds a reference to a method.
Event = A notification mechanism based on a delegate.
Amit MohantyRank #112 · 20K reputationPosted Jun 3, 2025, 1:15 AM EDT
A delegate is a type-safe function pointer in .NET. It holds a reference to one or more methods that match its signature and return type. Think of it as a reference to a method with a specific signature.
Syntax of a delegate:
public delegate int MathOperation(int a, int b);This defines a delegate type called MathOperation that can reference any method that takes two int parameters and returns an int.
How to use Delegate:
// A method matching the delegate signature public static int Add(int x, int y) { return x + y; } public static void Main() { // Instantiate the delegate MathOperation op = new MathOperation(Add); // Invoke the delegate int result = op(3, 4); Console.WriteLine(result); // Output: 7 }