aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Delegates in C# | Use of Delegates in C#.Net with Example

Sep 4, 2014

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.


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 and many articles relating to c#,, 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 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

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,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Good one @suresh keep posting

Anonymous said...

very helpful, Thanks Suresh. Keep Posting

Anonymous said...

Really u r using very simple English which can be understandable by any one :)
thank u :)

Suresh Dasari said...

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......

Hareesh said...

Nice article!!! Could you please explain when we are going to use deletes in real time projects with example if possible.Thanks

JustAboutAI said...
This comment has been removed by the author.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.