Delegates using examples


Delegate is a form of type safe functional pointer. in compile time we don't know which method will be invoked. We can invoke a delegate asynchronous. Mostly it will be used in event drive programming. number of parameter, type of parameters and return type of a delegate should be match with method signature.

Delegate is a form of type safe functional pointer. in compile time we don't know which method will be invoked. We can invoke a delegate asynchronous. Mostly it will be used in event drive programming. number of parameter, type of parameters and return type of a delegate should be match with method signature.

There are three steps in defining and using delegates:

Declaration
Instantiation
Invocation

delegate
========

Declaration:
delegate void simpledelegate();

public void simplefun()
{
......
}
Instantiation:
simpledelegate sd = new simpledelegate(simplefunc);
Invocation:
sd();


Event is based on publishing and subscribing concept.
declare an event ( in user control)
================
public delegate void RefreshTreeHandler();
public event RefreshTreeHandler refreshtree;

protected void OnRefreshTree()
{
if(refreshtree != null)
{
refreshtree();
}
}

publishing an event in usercontrol
==================================
this.OnRefreshTree();


Subscribing an event
====================
objusercontrol.refreshtree += new usercontrol.refreshtreehandler(method);


Comments

Author: Sridhar Thota14 Dec 2015 Member Level: Gold   Points : 7

Delegates holds address of one or more methods.
These are used to hide information like class names and method names.
Delegates are used to create user defined events on fly as we needed in our code.
Delegates are of two types
1.Single cast delegate
2.Multi caste delegate
Sample code:


Class Sample
{
public void dosomething();
{
MessageBox.Show("Am from dosomething method");
}
}
public delegate void delegate_name();
code for btn_Click
{
Sample obj=new Sample();// creating object for delegate with address of method.
delegate_name obj2=new delegate_name(obj.dosomething);
obj2(); // call delegate object.
}

Multicaste delegate Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace multicastdelegatecustom
{
class multiDelegateSample
{
public delegate void delName(int x, int y);

public class A
{
public event delName eventName;

public void myEvent(int x, int y)
{
eventName(x, y);
Console.WriteLine("Custom Event Raised");
}

public void Add(int p, int q)
{
Console.WriteLine("Adding Method {0}", p + q);
}

public void Sub(int x, int y)
{
Console.WriteLine("Your Subtract Method {0}", p - q);

}
}

static void Main(string[] args)
{

A obj = new A();

obj.eventName+= new delName(obj.Add);

obj.eventName+= new delName(obj.Sub);

obj.myEvent(30, 10);

Console.ReadLine();

}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: