Harnessing C# Delegates: Invoking Methods Across Multiple Classes

Delegates in C# is the most powerful feature that one can use for various purposes.

Mostly developers use delegates for either for implementing design patterns or encapsulation or even for code reusability.

Delegates

Delegates

Let's consider the below class.

public class UserDetails : iUserDetails
{
    public void PrintName()
    {
        Console.WriteLine("The Name is Rohit");
    }
    public void PrintAge()
    {
        Console.WriteLine("The Age is 28");
    }
    public void PrintGender()
    {
        Console.WriteLine("The Gender is Male");
    }
}

The UserDetails class implements an interface iUserDetails and provides methods to print basic information about a user. It includes three methods: PrintName(), which prints the name of the user PrintAge(), which prints the age of the user, and PrintGender(), which prints the gender.

If you need to add another function in the above class, then you will have to rebuild the dll again. If the dll is used in other projects, you will have to add the dll reference again.

This is where a delegate comes into the picture. A delegate in C# is a type that represents references to methods with a specific signature and return type. It allows methods to be passed as parameters, enabling flexible and reusable code.

To demonstrate, with functions of the above class, we will be calling the functions of the below class where the UserContactDetails is a class that provides methods to print a user's contact information. It includes two methods: PrintEmail(), which prints the email of a user, and PrintPhone(), which prints a phone number.

internal class UserContactDetails
{
    public void PrintEmail()
    {
        Console.WriteLine("The Email is [email protected]");
    }
    public void PrintPhone()
    {
        Console.WriteLine("The Phone is 0987654321");
    }
}

We will be assigning all the methods from the UserContactDetails and UserDetails class to a delegate.

In the code snippet below, we have created a delegate and passed the delegate in a Print function as a parameter.

public delegate void Details();
internal class PrintUserDetails
{
    public void Print(Details obj)
    {
        obj();
    }
}

Assigning functions to a delegate

In the below code snippet, an instance of the UserDetails class objUser is created, and its methods PrintName, PrintAge, and PrintGender are assigned to a delegate delObj.

Similarly, an instance of the UserContactDetails class object is created, and its methods PrintEmail and PrintPhone are also added to the same delegate.

UserDetails objUser = new UserDetails();
// Assigning Functions from UserDetails class to delegate
Details delObj = objUser.PrintName;
delObj += objUser.PrintAge;
delObj += objUser.PrintGender;

UserContactDetails objCus = new UserContactDetails();
// Assigning Functions from UserContactDetails class to delegate
delObj += objCus.PrintEmail;
delObj += objCus.PrintPhone;

PrintUserDetails objPrint = new PrintUserDetails();
// Calling all the assigned functions to the delegate
objPrint.Print(delObj);

Finally, an instance of the PrintUserDetails class objPrint is created, and the Print method of objPrint is called with the delegate delObj as an argument.

This results in all the methods assigned to the delegate being invoked sequentially, printing the user's name, age, gender, email, and phone number to the output.

Microsoft

Output-Delegates

Check out the git repository and try yourself.

Conclusion

Delegates in C# offer a flexible way to work with methods, enabling more modular and reusable code. By mastering delegates, you can create dynamic and efficient solutions, making your applications more versatile and easier to maintain.