Introduction:
Here I will explain what is delegates in c#
and how to use delegates in your c#
project. In c we used pointers in C, delegates are
something like pointers in C but type safe. Previously we send data as
parameter but today we send methods as parameter through objects. In C# 2.0
delegates are introduced. It used to send methods to pass through its object it’s
like a reference variable to the reference of the method which you have to
execute. It is derived from
System.Delegate class and generally use for implementing events and call back
method.
Description:
In previous articles I explained constructors in c#, polymorphism example in c#, delegates in c# with example, Difference b/w compile time and runtime polymorphism, sealed classes in c#.net and many articles relating to c#,
vb.net,
code snippets. Now I will explain what is delegates in c#
and how to use delegates in your c#
project.
I will show you how to declare a delegate and how to
execute through the object of the delegate.
Declaration
of delegate in C#
If we want to replace all special characters in string
with spaces we need to write code like as shown below
public delegate int delegate1 (string
input);
public delegate void delegate2 (string
input);
public delegate string delegate3 (string
input);
|
In above code we declared two delegates first one for a
method which takes an input string and return an integer value and second one
is taking an input string as first one but returning nothing(void) and third
and the last one denotes a method which takes string also return string.
So is clear how to declare different delegate. Now let’s
check how to call the delegates. To call these delegates we need to create
objects for these three delegates. Let’s create three objects of these three.
public delegate int delegate1(string input);
public delegate void delegate2(string input);
public delegate string delegate3(string input);
delegate1 obj1 = new delegate1(methodReturnInt);
delegate2 obj2 = new delegate2(methodReturnVoid);
delegate3 obj3 = new delegate3(methodReturnString);
|
So objects are created for these different delegates
those are methodReturnInt,
methodReturnVoid, methodReturnString these are the method name which are
called by the three delegate objects.
These three methods are like as shown below
// return int
public static int methodReturnInt(sting ip)
{
return 0;
}
// return void
public static void methodReturnVoid(sting ip)
{
return;
}
// return string
public static string methodReturnString(sting ip)
{
return "Hi !";
}
|
Now let’s see how to call through the objects
static void Main(string[] args)
{
// creating objects
delegate1 obj1 = new delegate1(methodReturnInt);
delegate2 obj2 = new delegate2(methodReturnVoid);
delegate3 obj3 = new delegate3(methodReturnString);
// calling methods with value
obj1("abc");
obj1("pqr");
obj1("xyz");
}
|
I hope it’s clear to you how to
use delegates in your project after this short session of coding. Try it
yourself and bang it. Happy coding......
Arkadeep De
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
Good one @suresh keep posting
very helpful, Thanks Suresh. Keep Posting
Really u r using very simple English which can be understandable by any one :)
thank u :)
Guys this article is a Guest Post which was written by Arkadeep De this credit goes to him...thanks Arkadeep De for your valuable contributions......
Nice article!!! Could you please explain when we are going to use deletes in real time projects with example if possible.Thanks
Note: Only a member of this blog may post a comment.