Monday, November 1, 2021

C# Delegates and their types

C# Delegates and Their Types

A delegate is a type in C#. Similar to classes and interfaces, a delegate is a type defined in the C# programming language.

Type and Namespace

In C#, any type is declared and defined within a namespace. Just like classes and interfaces are defined within a namespace, delegates can also be declared and defined within a namespace. However, it is not mandatory to define delegates only at the namespace level. Delegates can also be declared within a class.

When one type contains another type, it is called a nested type. For example, if there is a class defined inside another class, it is called a nested class. Similarly, delegates can be declared inside a class.

Nested Types

If a type is contained within another type, it is referred to as a nested type.


What Are Delegates?

A delegate is a type that allows methods to be called indirectly. There are three main ways to invoke methods in a class:

  1. Non-static methods: Called using an instance of the class.
  2. Static methods: Called using the class name directly.
  3. Delegates: Serve as a "pointer" to a method, allowing indirect invocation.

How Delegates Work

The methods a delegate can call are determined by its declaration. A delegate declaration resembles a method signature. Like a method, a delegate has a return type, a name, and parameters. The difference is that instead of specifying a method name, the delegate is given a name and uses the delegate keyword before the return type.

For example:

public void Add(int n1, int n2);

public void Sub(int n1, int n2);

public void Multiply(int n1, int n2);

The corresponding delegate declaration:

public delegate void CalcDelegate(int num1, int num2);

This delegate can refer to any method whose signature matches its own, i.e., the same return type, number, type, and order of parameters.

Delegate Example

CalcDelegate delAdd = new CalcDelegate(Add);

CalcDelegate delSub = new CalcDelegate(Sub);

CalcDelegate delMultiply = new CalcDelegate(Multiply);

Here:

  • delAdd is a delegate object holding a reference to the Add method.
  • delSub holds a reference to Sub, and so on.

When creating a delegate instance, only the method name is passed as a parameter—not the values for the method's parameters.

To call a method using a delegate:

delAdd.Invoke(2, 4);

delSub.Invoke(2, 4);

delMultiply.Invoke(2, 4);

Alternatively, you can use the shorthand:

delAdd(2, 4);

delSub(2, 4);

delMultiply(2, 4);


Multicast Delegates

A single delegate instance typically refers to one method. However, a multicast delegate can hold references to multiple methods and invoke them sequentially. This is achieved using the += operator.

For example:

CalcDelegate delObj = Add;

delObj += Sub;

delObj += Multiply;

delObj.Invoke(2, 5);

In this example, delObj calls the Add, Sub, and Multiply methods in sequence. Note that you can assign methods to a delegate instance without using the new operator.


Anonymous Methods

An anonymous method is a method that does not have a name. Instead, its logic is defined directly within the delegate.

For example:

CalcDelegate del = delegate (int x, int y)

{

    Console.WriteLine(x + y);

};

del(3, 5); // Output: 8

Here, the delegate keyword is used to define the anonymous method, and the method's body is enclosed in curly braces {}.


This article introduced you to the basics of delegates, including their declaration, invocation, multicast usage, and anonymous methods. Delegates are a powerful feature in C# that enable flexibility and dynamic method invocation, which is particularly useful in event-driven programming and callback scenarios.

 


No comments:

Post a Comment

Hot Topics