Multicast Delegates and Events
Multicast Delegates and Events
Written by Mike James   
Friday, 24 June 2016
Article Index
Multicast Delegates and Events
Events
Generic Events

Generic and Standard Events

Of course most of us simply make use of predefined events but there has been a change in the way that this works. Originally we needed a delegate type for each even slightly different even or we just passed object types to allow the event handler to work with a range of types. A better solution is to use generics and this is the approach now taken by the framework classes.

coverCsharp

For example, the original standard event handler was no generic:

public delegate void EventHandler(
                     object sender, EventArgs e);

Using object as the first parameter allowed any class to raise the event and still notify the users of the event handlers what had raised the event. The new generic version is:

public delegate void
 EventHandler<TEventArgs>
  (object sender, TEventArgs e)
       where TEventArgs : EventArgs;

which still leaves the sender untyped. A better version is:

public delegate void
   GenericEventHandler<S,A>
      (S sender,A args);

In this case the event would be set up using something like:

public event GenericEventHandler
          <MyClass,MyArgs> MyNewEvent;

Generics significantly simplify the implementation of events and by reducing the need to pass an object type increase overall type safety.

 

Deep C#

 coverCsharp

 Chapter List

  1. Value And Reference
    Value and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

  2. Passing Parameters
    Passing parameters is easy as it always works in the same way but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works.

  3. Inheritance
    Inheritance is a simple idea, until you try to make use of it.

  4. Casting – the escape from strong typing
    Casting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it then the how makes a lot more sense.

  5. Controlling Inheritance
    Inheritance is a great idea but it is a powerful technique that can be misused. C# provides the tools to keep inheritance under control or to turn it off completely if you want to.

  6. Delegates
    Delegates are C#'s original way of allowing you to work with functions as if they were first class objects. The aim may be simple but the need to define a type and then an instance of the type can be confusing. Let's see if we can make it all seem logical.

  7. Multicast delegates and events
    Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

  8. Take Exception To Everything

  9. Dangerous Pointers
    In C# pointers have been replaced by references but there is still a need for the raw dangerous original concept. You could say "what's so dangerous about pointers?"
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

blog comments powered by Disqus 

<ASIN:0321658701>
<ASIN:1451531168>

<ASIN:0321637003>

<ASIN:0596800959>

<ASIN:047043452X>

<ASIN:0123745144>



Last Updated ( Friday, 29 July 2016 )
 
 

   
Banner
RSS feed of all content
I Programmer - full contents
Copyright © 2016 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.