Using the delegate variables

Let's learn about delegates.

What is a delegate?

A delegate is a variable that can store a method and can then be passed around as needed. Previously, the variables discussed held data such as strings or objects. Methods can be treated the same way. In the example below, a delegate named MathExample can store methods that accept two int values and return a string value.

Syntax: Creating a delegate

public delegate string MathExample(int num1, int num2);

Syntax: Using a delegate

MathExample calculateMath = AddNumbers; // Store method AddNumbers

Example

Get hands-on with 1400+ tech skills courses.