Skip to content

Declaration

Context: Declare a delegate using the delegate keyword, specifying the return type and parameter types.

[access modifier] delegate returnType DelegateName(parameters);
public delegate void PrintMessage(string message);
public delegate int Calculator(int a, int b);

Delegates are reference types. You can declare them at namespace or class level.

public class MathOperations
{
public delegate double Operation(double x, double y);
}

Defining custom callbacks: In UI frameworks, you might declare a delegate for validation callbacks (e.g., delegate bool ValidateInput(string input)).

Example: In ASP.NET Core, custom middleware often uses delegate declarations for request handling.