C# Interview Question #54

What is a delegate in C#?

Exceptions, Delegates, Events & Lambdas Mid-Level Intermediate

Quick Interview Answer

A delegate is a type-safe reference to one or more methods with a compatible signature. It allows methods to be passed around as values and invoked indirectly.

Detailed Explanation

A delegate is a type-safe reference to one or more methods with a compatible signature. It allows methods to be passed around as values and invoked indirectly.

Delegates are the foundation of events and are also used extensively with lambda expressions, callbacks, LINQ, and functional-style APIs.

A delegate declaration specifies the required parameter types and return type.

Code Example

public delegate int Calculate(int a, int b);

static int Add(int a, int b) => a + b;

Calculate operation = Add;
int result = operation(10, 20);