Learn About Single Cast Delegates In C#

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
  1. public delegate strin gmyDelegates(string FirstName, string LastName);  
  2. public class SubClass {  
  3.     public string UserName(string FirstName, string LastName) {  
  4.         return FirstName + "" + LastName + "";  
  5.     }  
  6. }  
  7. public class Personal {  
  8.     static void Main(string[] args) {  
  9.         SubClass subClass = newSubClass();  
  10.         myDelegates myDelegates = newmyDelegates(subClass.UserName);  
  11.         myDelegates("Manikandan""M");  
  12.     }  
  13. }  
  14. //Output : Manikandan M  

Conclusion

 
This article will help you to understand the single cast delegates in C# using simple programming.