dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersSidharthan
prafulla
Nanda Kishore
pragna
Shine S
Navneet
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » General

Delegates and Events


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 12



 


What is a delegate


A delegate can be considered as a class that encapsulates methods. An instance of a delegate can refer to one or methods.
Eg.

delegate void MyDelegate();

The above code declares a delegate, the objects of which can refer to methods.




void Method1()
{
//this is a method
}


void Method2()
{
//this is another method
}



//here we are creating a a new object and attaching Method1 to it.
MyDelegate dlg1 = new MyDelegate(Method1);


//now attaching another method

dlg1+= Method2;


//If you call the delegate now, all the methods refered by it will be executed.


dlg1( );

//Method1 and Method2 are executed in the same order as they are attached to the delegated.





Delegates are very much usefult to handle events. They can act like a store where the handlers of events are stored. The usage of delegates alongwith events provides a way to dynamically add or remove event handlers at runtime.


Here is the complete code.

using System;

namespace ConsoleApplication45
{
class SalesItem
{
int stock;

public delegate void onLowStock(); //delegate that holds StockLow events
public event onLowStock StockLow; //StockLow event is attached to the delegate onLowStock

public SalesItem(int stock)
{
this.stock = stock;
}

public void Add(int qty)
{
stock += qty;
}

public void Remove(int qty)
{
if (stock < qty)
{
StockLow(); //calling the event
}
stock -= qty;
}
}
class Program
{
static void Main(string[] args)
{

SalesItem s1 = new SalesItem(10);
s1.StockLow += new SalesItem.onLowStock(s1_StockLow); //new event handler is attached to the StockLow event
s1.Remove(5);
s1.Add(2);
s1.Remove(25);


}

static void s1_StockLow()
{
Console.WriteLine("Item s1 is out of stock");
}
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "Delegates and Events"
Author: SIVAKUMAR A    06 Jul 2012Member Level: Bronze   Points : 0
Can you please explain more about need of using delegates and events.Because same thing we can do by using interfaces and classes.

Please give advantages of using events nd delegates.



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Migration from AS400 (Small/Mid level Mainframe) to .NET
    Previous Resource: Random Numbers
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Events  .  How to create a custome event?  .  Uses of Delegates  .  Delegates  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.