C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
A delegate in C# is a type that represents references to methods with a particular parameter list and return type. You can think of delegates as function pointers in C++. They allow you to define a variable that can hold a reference to a method, and then call the method through this reference. This can be useful for scenarios such as event handling and callbacks.
In this tutorial, we'll cover the basics of named method delegates in C#. Here's what we'll cover:
Let's dive in!
To declare a delegate, use the delegate
keyword followed by the return type, the delegate name, and the parameter list (in parentheses).
delegate void SimpleDelegate();
This declares a delegate called SimpleDelegate
that represents methods with no return value and no parameters.
Now, let's create a named method with the same signature as our delegate. For this tutorial, we'll create a simple method that just prints a message:
public class Program { public static void DisplayMessage() { Console.WriteLine("Hello, this is the named method!"); } }
To assign a named method to a delegate, create a delegate instance and then assign the method to it:
public static void Main(string[] args) { SimpleDelegate myDelegate = DisplayMessage; }
Now, myDelegate
holds a reference to the DisplayMessage
method.
To invoke the method referenced by the delegate, simply call the delegate like you would call a regular method:
public static void Main(string[] args) { SimpleDelegate myDelegate = DisplayMessage; myDelegate(); // This will call the DisplayMessage method }
When you run the program, you'll see the following output:
Hello, this is the named method!
That's it! You've learned the basics of named method delegates in C#. They provide a convenient way to store and invoke methods in a flexible and type-safe manner.
How to define named delegates in C#
Named delegates are defined using the delegate
keyword, specifying the return type and parameters.
delegate int MathOperation(int x, int y);
Using named delegates for method references in C#
Named delegates allow referencing methods by creating instances of the delegate type.
MathOperation add = Add; int result = add(3, 4);
Named delegates vs. anonymous delegates in C#
Named Delegate:
delegate void MyDelegate(string message);
Anonymous Delegate:
MyDelegate anonymousDelegate = delegate (string msg) { Console.WriteLine(msg); };
Declaring and invoking named delegates in C#
delegate void MyDelegate(string message); class Program { static void Main() { MyDelegate myDelegate = DisplayMessage; myDelegate("Hello, Named Delegates!"); } static void DisplayMessage(string message) { Console.WriteLine(message); } }
Delegate as a method parameter in C#
Named delegates can be passed as method parameters, allowing dynamic behavior.
void PerformOperation(int x, int y, MathOperation operation) { int result = operation(x, y); Console.WriteLine($"Result: {result}"); }
C# named delegate example
delegate void MyDelegate(string message); class Program { static void Main() { MyDelegate myDelegate = DisplayMessage; myDelegate("Hello, Named Delegates!"); } static void DisplayMessage(string message) { Console.WriteLine(message); } }
Multicast named delegates in C#
Named delegates can reference multiple methods, creating a multicast delegate.
MathOperation add = Add; MathOperation subtract = Subtract; MathOperation multiDelegate = add + subtract;
Creating and assigning named delegates in C#
Assign methods to named delegates using the +=
operator.
MathOperation add = Add; add += Subtract;
Event handling with named delegates in C#
Named delegates are often used for event handling in C#.
class EventPublisher { public event MyDelegate MyEvent; }
C# named delegates vs. lambda expressions
Named Delegate:
delegate void MyDelegate(string message);
Lambda Expression:
Action<string> myLambda = message => Console.WriteLine(message);
Delegate types and signatures in C#
The delegate type defines the method signature it can reference.
delegate void MyDelegate(string message);
C# generic named delegates
Generic named delegates can work with a variety of data types.
delegate T MyGenericDelegate<T>(T value);
Named delegates and design patterns in C#
Design patterns like the Observer Pattern often use named delegates for event notification.