Introduction
In this article, I will explain about single delegates in C#. Single cast delegates only hold the reference of a single method. A single cast delegate derives from the System. Delegate class. It’s used to invoke a single method at a time.
Syntax of delegates
[access_modifier] delegate [return type] [delegate name] ([parameters])
Declarations
public delegate TypeOfDelegate Delegate_Name();
Examples
public delegate string myDelegates(string FirstName, string LastName);
Single cast delegates have some of the features in C# as below
- We can pass the method as a parameters
- It is type-safe and this is similar to C++ function pointers.
- It helps us to Invoke and define the callback methods
Sample Program
- public delegate strin gmyDelegates(string FirstName, string LastName);
- public class SubClass {
- public string UserName(string FirstName, string LastName) {
- return FirstName + "" + LastName + "";
- }
- }
- public class Personal {
- static void Main(string[] args) {
- SubClass subClass = newSubClass();
- myDelegates myDelegates = newmyDelegates(subClass.UserName);
- myDelegates("Manikandan", "M");
- }
- }
-
Conclusion
This article will help you to understand the single cast delegates in C# using simple programming.