1 / 14

C# Delegates and Events

C# Delegates and Events. CS-410, Fall 2004 Michael Weiss. C# Delegates and Events . method. delegate. method. invocation list. method. A delegate in C# contains one or more method references: its invocation list . Invoking the delegate invokes the methods in its invocation list.

morse
Télécharger la présentation

C# Delegates and Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Delegates and Events CS-410, Fall 2004 Michael Weiss CS410 – Software Engineering C# Delegates and Events

  2. C# Delegates and Events method delegate method invocation list method • A delegate in C# contains one or more method references: its invocation list. • Invoking the delegate invokes the methods in its invocation list. • Delegates are primarily used for event handling. CS410 – Software Engineering C# Delegates and Events

  3. Basic delegate pattern in C# declare a delegate type delegate void MyDelegate(int x); • class Eater { • static void EatAnInt(int x) {…} • void ConsumeAnInt(int x) {…} some methods for the invocation list • static void Main() { • Eater etr = new Eater(); • MyDelegate d = new MyDelegate(EatAnInt) + new MyDelegate(etr.ConsumeAnInt); create a delegate • d(5); • } • } invoke it; both EatAnInt(x) and etr.ConsumeAnInt(x) will be invoked CS410 – Software Engineering C# Delegates and Events

  4. Example from C# delegate tutorial // Declare a delegate type for processing a book: public delegate void ProcessBkDelegate(Book book); // Call a passed-in delegate on each book public void ProcessBooks(ProcessBkDelegate processBk) { foreach (Book b in list) { processBk(b); } } static void PrintTitle(Book b) {…} bookDB.ProcessBooks(new ProcessBkDelegate(PrintTitle)); PriceTotaller totaller = new PriceTotaller(); bookDB.ProcessBooks( new ProcessBkDelegate(totaller.AddBookToTotal)); CS410 – Software Engineering C# Delegates and Events

  5. Event Handling in Java • Events are not explicitly supported in Java. • There is a base class java.awt.Event, for GUI events. • There is a standard pattern (the Observer pattern) used for Event handling. • Example: java.awt.Button is a source of ActionEvents, and ActionListeners listen for ActionEvents • … but first, the ActionListener has to register with the source of the ActionEvents CS410 – Software Engineering C# Delegates and Events

  6. Example: Java GUI Event Handling • class java.awt.Button { • public void addActionListener(ActionListener l); • } • interface ActionListener { • void actionPerformed(ActionEvent e); • } • class MyButtonHandler implements ActionListener{ • void actionPerformed(ActionEvent e) { • System.out.println("Click!!"); • } • } • … Button b = new Button(); • b.addActionListener(new MyButtonHandler()); CS410 – Software Engineering C# Delegates and Events

  7. Event Handling: Observer Pattern register new eventInfo notify(eventInfo) query aSource aListener CS410 – Software Engineering C# Delegates and Events

  8. Event Handling: Java and C# Java: Event source and listener are both class-instances. Listener registers for event notification by calling a method (e.g., addListener). Listener has a method to handle the event (e.g., actionPerformed. Event info is stored in an Event object. C#: Event source is a class-instance; listener is a method. Event source has a delegate member to store listeners. Keyword event is used to declare this member. Listener registers by adding itself to the delegate's invocation list. Event info is stored in an EventArgs object. CS410 – Software Engineering C# Delegates and Events

  9. Example: C# Event Handling public delegate void FlashHandler(object sender,EventArgs e); class Thunderstorm { public event FlashHandler Lightning; } • class Weatherman { • static void CountFlashes(object sender, EventArgs e) {…} • public static void Main() { • Thunderstorm storm = new Thunderstorm(); • storm.Lightning += new FlashHandler(CountFlashes); • } • } CS410 – Software Engineering C# Delegates and Events

  10. Semantics of the event keyword • Differences between • public eventDelegateTypeFooEvent • publicDelegateTypeFooDelegate • Event can be invoked only from inside the class: • obj.FooEvent() is illegal! • Even subclasses cannot directly invoke event! • Outside the class, can only change invocation list: • obj.FooEvent += new DelegateType(…); • obj.FooEvent -= new DelegateType(…); • Events can be declared in an interface. CS410 – Software Engineering C# Delegates and Events

  11. Inheritance and C# events • Since subclasses cannot invoke events, one often creates a method to invoke it: • event ClickHandler Click; • public OnClick() { • if (Click!=null) Click(); • } • event ClickHandler Click; • public virtual OnClick() { • if (Click!=null) Click(); • } • This is also a good place to check that there are listeners: "if (Click != null) …" CS410 – Software Engineering C# Delegates and Events

  12. .NET Framework Event Guidelines • C# allows you to use any delegate type for event: • delegate MyReturnType MyDelegateType( MyArgType);event MyDelegateType MyEvent; • The .NET framework provides two system types: • System.EventArgs (used for event info)System.EventHandler (delegate type) • Signature of EventHandler: • delegate void EventHandler( object sender, EventArgs e); CS410 – Software Engineering C# Delegates and Events

  13. .NET Framework Event Guidelines • In most cases, simply use EventHandlerfor your events: event EventHandler MyEvent; • You pass in the source of the event as the sender, and you use Empty for the event args:MyEvent(this, EventArgs.Empty); • If you need to pass more event info, you declare a subclass of EventArgs: class MyEventArgs:EventArgs {...} delegate void MyHandler(object src, MyEventArgs e); event MyHandler MyEvent; CS410 – Software Engineering C# Delegates and Events

  14. Summary • Delegates enable you to invoke methods indirectly. • Both static and instance methods can be invoked. • Delegates can do multi-casting (the invocation list). • An event is a delegate member of a class. • The invocation list of an event contains the event handler methods. • An event can be invoked only from the class that declares it (not even from a subclass). • The .NET framework places additional restrictions on how events should be used (EventHandler, EventArgs). CS410 – Software Engineering C# Delegates and Events

More Related