Delegates in C#


Introduction

Delegates in C# are like functions pointers in C/C++. A multi cast delegate can refer to several methods. A delegate can be used to invoke a method, the call to which can only be resolved or determined at runtime. This article discusses what delegates are and how they can be used in C# with lucid code examples.

What is delegate

Delegates are function pointers in C# that are managed and type safe and can refer to one or more methods that have identical signatures. Delegates in C# are reference types. They are type safe, managed function pointers in C# that can be used to invoke a method that the delegate refers to. The signature of the delegate should be the same as the signature of the method to which it refers. According to MSDN, "A delegate in C# is similar to a function pointer in C or C++. 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 which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure."

C# provides support for Delegates through the class called Delegate in the System namespace. Delegates are of two types.

  • Single-cast delegates
  • Multi-cast delegates

A Single-cast delegate is one that can refer to a single method whereas a Multi-cast delegate can refer to and eventually fire off multiple methods that have the same signature.

Some things about delegates:

  1. They must be public.
  2. They can have single event or multiple event.

Now we jump on the programming part how to use delegate

Now we see how to declare the delegate:

The sentence is :

public delegate Int64 mydelegate(Int32 a, Int32 b);
public delegate void MypersonalDelegate();

Now we create the event to use this delegates, the syntax for event is like,

public event mydelegate MyEvent;
public event MypersonalDelegate MyPersonalEvent;

Now we define the event,

MyEvent += delegate(Int32 a, Int32 b)
        {
            Response.Write("Multiplication Is : ");
            Response.Write(a * b);
            Response.Write("<br/><br/>");
            return 1;
        };

If we want to define multiple event than also we can define.

MyEvent += delegate(Int32 a, Int32 b)
        {
            Response.Write("Multiplication Is : ");
            Response.Write(a * b);
            Response.Write("<br/><br/>");
            return 1;
        };

        MyEvent += delegate(Int32 a, Int32 b)
        {
            Response.Write("Addition Is : ");
            Response.Write(a + b);
            Response.Write("<br/><br/>");
            return 1; };

Now we jump towards the usercontrols how to use delegate with user controls because mostly delegates are going to use with usercontrols.

Just the same thing as we have declare delegate over here same way we need to declare that in usercontrols .cs file.

     public delegate void MypersonalDelegate();

    public event MypersonalDelegate MyPersonalEvent;

Now from the page where we have put the user controls we need to write events body,

WebUserControl1.MyPersonalEvent += delegate()
        {
            string str = Request.Url.ToString();
            Response.Write(str);
        };

My usercaotrol is

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

So now it is good to manage lots of scenarios.

Just imagine the scenario where you have same design in 4-5 pages but their functionality or code is different.
At that time we can use it nicely. As the code for button is not written in the usercontrols.cs file but it is written in the aspx.cs file so we can use same design with different codes.

I have uploaded my demo source code download that and check out the delegates.


Similar Articles