Current area: HOME -> An Introduction To Delegates
|
An Introduction To Delegates
Delegates
In this article we will deal with Delegates in C#. I purposely tried to keep this article very simple so that people could understand the basics of delegates. After this I will extends its boundaries towards Events in DotNet.
Delegates in C# are objects which points towards a function which matches its
signature. Delegates are reference type used to encapsulate a method with a
specific signature. In C++ you accomplish this task by having pointer to
a function. Unlike pointers delegates are type-safe. A delegate in C# allows
you to pass a methods of class to objects of other classes. You can pass
a method Print in class A class B by wrapping it in a delegate and class B is able to access the method of class A.
Functionality of Delegates can be accomplished in four steps.
- In order to create a delegate its signature must match the signature of the object you are trying to encapsulate in a delegate. For e.g we consider a simple function Print(). Its signature is like
Public void print()
{
Console.Writeline("Hello")
}
Now the signature of the delegate must match the signature of method above.
Public void Mydelegate();
Look the declaration of delegate it does not return anything nor it takes anything as argument. This doesn't mean that we cannot declare delegate which can take argument and return arguments.
A delegate which takes an argument and return string.
public string MySecondDelegate(int number);
furthermore delegate can encapsulate Instance Method as well as static methods which match its signature.
-
In this step define all the methods which has the same signature as the delegate defined.
- In this step create the delegate object and pass the method as a parameter
to delegate.
Mydelegate l_objMyDelegate=new Mydelegate(print);
the method print encapsulated
- Now call the encapsulated method using delegate object.
Diagram Explaining the Delegate.
Practical Implementation
Main Screen
First Button Pressed that is Pentium!
Second Button Pressed that is Pentium!!
C# Implementation
Code of Button Pentium!:
private void btnPentiumI_Click(object sender, System.EventArgs e)
{
Computer l_objComputer=new Computer();
ComputerType l_objCallComputer=new
ComputerType(l_objComputer.PentiumI);
l_objCallComputer();
}
Code of Button Pentium!!:
private void btnPentiumIISS_Click(object sender, System.EventArgs e)
{
Computer l_objComputer=new Computer();
ComputerType l_objCallComputer=new
ComputerType(l_objComputer.PentiumII);
l_objCallComputer();
}
If you look at the code you will notice that I have created an object of class Computer and then I also created an object of my Delgate that is ComputerType and then encapsulate or pass my desired function as its parameter the I just called the delegate which in return execute the given method.
Code of computer class:
using System;
using System.Windows.Forms;
namespace delegatesprac
{
///
/// Summary description for Computer.
///
public class Computer
{
///
/// Method petium! that has the similar
/// signature as Delegate ComputerType
///
public void PentiumI()
{
MessageBox.Show("Configuration: Pentium! 200MMX "+
"2.1 GB HardDrive "+
"32 MB RAM "+
"32X CD-ROM");
}
///
///Method petium!! that has the similar
/// signature as Delegate ComputerType
///
public void PentiumII()
{
MessageBox.Show("Configuration: Pentium!! 350MMX "+
"4.3 GB HardDrive "+
"64 MB RAM "+
"40X CD-ROM");
}
}
///
/// Delegate Computer type
///
public delegate void ComputerType();
}
Description
The above program defines a class Computer it has two methods Pentium! And Pentium!!. In the same file I defined the delegate that is ComputerType. Its has similar signature as the function Pentium! And Pentium!!. Now the coding of buttons Pentium! and Pentium!!. If you look at the code you will notice that I have created an object of class Computer and then I also created an object of my Delgate that is ComputerType and then encapsulate or pass my desired function as its parameter the I just called the delegate which in return execute the given method.
About the author
Faisal Jawaid is a software engineer in Pakistan and working in professinal enviroment for a year. He has programming experience of Java,Vb6 Scripting Languages like javaScript, HTML,CSS etc.Currently working on DotNet technologies like C# and VbDotNet.
|