Simple Delegates With Examples in C#

Simple Delegates With Examples.

Delegates have the following properties:

  1. Delegates are similar to C++ function pointers, but are type safe.
  2. Delegates allow methods to be passed as parameters.
  3. Delegates can be used to define callback methods.
  4. Delegates can be chained together; for example, multiple methods can be called on a single event.
  5. Methods don't need to match the delegate signature exactly.
  6. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code that can call the referenced method, without having to know at compile time which method will be invoked.
  7. An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.

Example 1

Before applying the delegate:

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.   
  4.     if (!IsPostBack)  
  5.     {  
  6.         GetData("Mahesh");  
  7.     }  
  8. }  
  9. public void GetData(string Name)  
  10. {  
  11.     lblName.Text = Name;  
  12. }  

Output

Mahesh

After applying the delegate:  

  1. public delegate void MyDelegare(string var);  
  2. protected void Page_Load(object sender, EventArgs e)  
  3.   
  4. {  
  5.     if (!IsPostBack)  
  6.     {  
  7.         MyDelegare objMyDelegare = new MyDelegare(GetData);  
  8.         objMyDelegare("Mahesh");  
  9.     }  
  10. }  
  11. public void GetData(string Name)  
  12. {  
  13.     lblName.Text = Name;  
  14. }  

Output

Mahesh

Multicast Delegate

It is a delegate that holds the reference of more than one method.

Multicast delegates must contain only methods that return void, else there is a run-time exception.

Example 2

  1. public delegate void MyDelegare(string var);  
  2. protected void Page_Load(object sender, EventArgs e)  
  3. {  
  4.   
  5.     if (!IsPostBack)  
  6.     {  
  7.         MyDelegare objMyDelegare = new MyDelegare(GetData);  
  8.         objMyDelegare += new MyDelegare(GetDat_one);  
  9.         //GetData and GetDat_one is called  
  10.         objMyDelegare("Mahesh");  
  11.         objMyDelegare -= new MyDelegare(GetDat_one);  
  12.         lblName.Text = lblName.Text + "<br />";  
  13.         //GetData is called  
  14.         objMyDelegare("Mahesh");  
  15.     }  
  16. }  
  17.   
  18. public void GetData(string Name){   
  19.     lblName.Text = lblName.Text + "GetDate : " + Name;  
  20. }  
  21.   
  22. public void GetDat_one(string Name)  
  23. {  
  24.     lblName.Text = lblName.Text + " GetDate_One : " + Name;  
  25. }  

Output

GetDate : Mahesh GetDate_One : Mahesh
GetDate : Mahesh

Up Next
    Ebook Download
    View all
    Learn
    View all