Delegate in C#

manjunathmh

Definition:

Delegate is type which  holds the method(s) reference in an object. It is also reffered as a type safe function pointers.

Advantages:

  • Encapsulating the method’s call from caller.
  • Effective use of Delegat improves the performance of application.
  • Used to call a method asynchronously.

Declaration:

public delegate type_of_delegate delegate_name()

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

Note:

  • you can use delegeate without parameter or with parameter list.
  • you should follow the same syntax as in the method.

(if you are reffering the method with two int parameters and int return type the delegate which you are declaring should be the same format.This is how it is reffered as type safe function pointer)

Sample Program using Delegate :

public delegate double Delegate_Prod(int a,int b);

class Class1 {

  static double fn_Prodvalues(int val1,int val2)       {      return val1*val2;       }   static void Main(string[] args)   {   

   //Creating…

View original post 350 more words

Advertisements

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