RSS

Delegates in C# .NET (Simple Session)

11 Jan

Delegate’s definition:

A delegate is a type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke (or call) the method through the delegate instance.

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.

Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs.

How to declare delegate?

public delegate type_of_delegate delegate_name()

Example:
public delegate int mydelegate(int delvar1, int delvar2)

//Notice please,

//You can use delegates without parameters or with parameter list.
//You should follow the same syntax as in the method.
//(If you are referring to the method with two int parameters and int return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)

The following program using a simple Delegate:

namespace  MyApp
{
class Class1
{

public delegate double del(double r, double y);

public double Sum(double x, double y)
{
return x + y;
}
public void FireDelegate()
{
del p = new del(Sum);
System.Windows.Forms.MessageBox.Show(p(5,10.5).ToString());
}
}
}

Multicast Delegate:

What is Multicast Delegate?
It is a delegate which holds the reference of more than one method. Multicast delegates must contain only methods that return void, else there is a run-time exception.

Being able to point to member functions is nice, but there are more tricks you can do with delegates. In C#, delegates are multicast, which means that they can point to more than one function at a time (that is, they’re based off the System.MulticastDelegate type). A multicast delegate maintains a list of functions that will all be called when the delegate is invoked.

namespace  MyApp
{
class Class1
{

public delegate double del(double r, double y);

public double Subtract(double x, double y)
{
return x – y;
}

public double Sum(double i, double o)
{
return i + o;
}

public Class1()
{
del p = new del(Subtract);
p +=  new del(Sum);
System.Windows.Forms.MessageBox.Show(p(5, 10.5).ToString());  // Subtract and Sum are called  
p -= new del(Sum);
System.Windows.Forms.MessageBox.Show(p(5, 10.5).ToString());  // Only Subtract is called
}
}
}

 Advantages Of Delegates:

  • Delegates are managed function pointers. they are type checked and held in spaces that can be reclaimed by the memory manager.
  • Encapsulating the method’s call from caller
  • Effective use of delegate improves the performance of application
  • Used to call a method asynchronously

 Delegates vs. Interfaces

Delegates and interfaces are similar in that they enable the separation of specification and implementation. Multiple independent authors can produce implementations that are compatible with an interface specification. Similarly, a delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification. When should you use interfaces, and when should you use delegates?

Delegates are useful when:

  • A single method is being called.
  • A class may want to have multiple implementations of the method specification.
  • It is desirable to allow using a static method to implement the specification.
  • An event-like design pattern is desired (for more information, see the Events Tutorial).
  • The caller has no need to know or obtain the object that the method is defined on.
  • The provider of the implementation wants to “hand out” the implementation of the specification to only a few select components.
  • Easy composition is desired.

Interfaces are useful when:

  • The specification defines a set of related methods that will be called.
  • A class typically implements the specification only once.
  • The caller of the interface wants to cast to or from the interface type to obtain other interfaces or classes.
 
2 Comments

Posted by on January 11, 2012 in Visual C# . NET

 

Tags: , , , , , ,

2 responses to “Delegates in C# .NET (Simple Session)

  1. Alex Leblois

    May 22, 2013 at 1:49 PM

    Hi,

    When we make an object of a class then we can access all function of a class and Base class functions too. Here we can see that a user can simple make an object of a

    class and access all information of class. But in real word this is drawback of an object.

    This is a nice article so thanks for sharing your knowledge. There are few other links that also described “Delegate in C#” in a good way.

    http://mindstick.com/Articles/2ace6310-943e-4f1b-a193-c1b5f61acd54/default.aspx?Delegate+in+C+Net

    http://www.dotnetperls.com/delegate

    http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: