Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Friday, 15 April 2016

Delegates in C# with Example

 No comments   

Delgates are a special type of function used in C#.These are type safe.Delegates are refrence type that holds the address or reference to a method. By using delegate we can pass function as a parameter. C# delegates are similar to function pointer in C++.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.
Delegates can be chained together; for example, multiple methods can be called on a single event.

Basic syntax for delegate declaration

delegate <return type> <delegate-name> <parameter list>

Steps for working with delegate:

Declare delegate type

public delegate void printMessage(string str);
  
 Here i declare a delegate which has void return type and takes 1 string type of parameter. Means we can pass reference of a method who takes the same return type and arguments like delegate.

Create object of Delegate

printMessage objDel=new printMessage(Your_Function_Name);

 In this step i create delegate object by using new keyword and main thing to understand we have to pass method as a parameter at the time of delegate object creation.

Call delegate

objDel("Your_Message");

Here is the close enough to understand delegate with a simple Example

public delegate void printMessage(string str);
protected void Page_Load(object sender, EventArgs e)

{
   
       printMessage objDel=new printMessage(MyMessage);
        objDel("Delegate Successfully Called ");
}
public void MyMessage(string Message)
{
    lblMessage.Text = Message

}

Multicast Delegate

A Multicast Delegate holds the reference of more than one method. But we can only call those methods who having the same return type like delgate otherwise  there is a run-time exception.
We can add multiple methods in single delegate object using  '+'  operator and remove using  '-' operator.
Example:

protected void Page_Load(object sender, EventArgs e)

{
   MyDelegate myDel = new MyDelegate(AddNumbers);
        myDel += new MyDelegate(MultiplyNumbers);
        myDel(10, 20);
}
   public void AddNumbers(int x, int y)
    {
        int sum = x + y;
        MessageBox.Show(sum.ToString());
    }

    public void MultiplyNumbers(int x, int y)
    {
        int mul = x * y;
        MessageBox.Show(mul.ToString());

    }
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write