What is C# Delegate

A function can have one or more parameters of different data types, 
but what if you want to pass a function itself as a parameter? 
How does C# handle the callback functions or event handler? 
The answer is - delegate.
A delegate is like a pointer to a function. It is a reference type
data type and it holds the reference of a method. 
All the delegates are implicitly derived from 
System.Delegate class.
Advantages
Encapsulating the method's call from caller
Effective use of delegate improves the performance of application
Used to call a method asynchronously
Declaration
public delegate type_of_delegate delegate_name()
Note
You can use delegates without parameters or with parameter list
You should follow the same syntax as in the method 
(If you are referring to the method with two int parameters and int return type,
the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)




Rahul Singh

As a passionate software developer, trainer, and tech blogger, I thrive on sharing knowledge and exploring the latest in technology. With a strong foundation in programming languages like C and C++, and expertise in platforms like Microsoft 365, SharePoint, and Azure, I aim to simplify complex concepts for others. My blog, Expert2Code.com, is a space where I document my learning journey, providing insights and resources for those eager to master the tech world. When I'm not coding or teaching, you'll find me reading, hiking, or discovering new tools and techniques to stay ahead in this fast-paced industry.

1 Comments

Previous Post Next Post