Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am beginner to .net and I studied about it but I am not getting the exact purpose
of them.I searched on net but the answers didnt cleared my doubt .Could you please clear my doubt?

Thanks
Prashant P
Posted
Updated 17-Sep-12 19:57pm
v2
Comments
[no name] 17-Sep-12 22:31pm    
okay.... and? What is that you are not "satisfied" with?
Sergey Alexandrovich Kryukov 18-Sep-12 14:05pm    
The problem is that "exact purpose" is something which not always can be defined, so I've chosen to explain this matter in greater detail, please see.
--SA
Prashant Bangaluru 18-Sep-12 1:58am    
I upadted the question.Now is that Ok?
Sergey Alexandrovich Kryukov 18-Sep-12 14:06pm    
It's better (see my comment above though). Anyway, after the improvement in this question I could have decided to explain this thing, please see.
--SA

1 solution

I don't think generic event handlers can exist. What you mean is probably the generic type System.EventHandler:
http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx[^].

The confusing part is: this is the generic delegate type called EventHandler, which is not an event handler itself, but a delegate type which, in particular, defines the signature of an event handler. And the event handler is always a method (named or anonymous) which is used to be added to the invocation type of an event instance. In addition to this confusion, the event instance type is not a type used in its declaration. It's the instance of some class. In other words, the declarations
C#
int Value; // the type of the object Value is int
//...
internal event EventHandler<MyEventArgs> SomethingHappened; // the type of SomethingHappened is NOT EventHandler<MyEventArgs>

their meaning is dramatically different: in first case, the type of the object is the same as declared type on the left of it; in the second case, the actual type of the event instance (object) and the declared type are unrelated.

Now, let's start from the very beginning and show the complete sample. Here is the declaration of event:
C#
public class MyEventArgs : System.EventArgs {
    internal MyEventArgs(/* some parameters ... */); // never needs to be public, because an event can be invoked in the declaring class only
    public // some properties specific to 
}

//...

public class MyEventDeclaringClass {
    public event EventHandler<MyEventArgs> SomethingHappened;

    internal InvokeWhenSomethingHappened() {
        if (SomethingHappened != null)
            SomethingHappened.Invoke(this, new MyEventArgs(/* appropriate arguments, will be used in event handler */));
    }
}


By using some public declarations above, I assumed that the class MyEventDeclaringClass and handling its event can be possible in the same assembly or on some other assembly. And now, the usage:

C#
MyEventDeclaringClass myInstance = new MyEventDeclaringClass(/* ... */);
//...

myInstance.SomethingHappened += (sender, eventArgs) => { // adding a handler to the event invocation list
   // use eventArgs properties passed when InvokeWhenSomethingHappened is called, see above
};
// the type of sender is System.Object (according to the code of InvokeWhenSomethingHappened, the run-time type is the instance MyEventDeclaringClass)
// the type of eventArgs is MyEventArgs
// the two types are known to the compiler from the type of myInstance.SomethingHappened via type inference

// the same thing without using the lambda syntax and type inference:
myInstance.SomethingHappened += delegate(System.Object sender, MyEventArgs eventArgs) { /* ... */ }


As you can see, the generic type System.EventHandler<> allows to abstract out the actual type of the event arguments and at the same time avoid type cast which would be needed for non-generic System.EventHandler. In other words, it is used for the same purposes generics are used for in other cases.

This is not required formally, but recommended way of using the mechanism of events. You need to use always two parameters, first should be object, and the second one should be the custom type always derived from System.EventArgs.

See also:
http://en.wikipedia.org/wiki/Anonymous_method[^],
http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx[^];

http://en.wikipedia.org/wiki/Type_inference[^],
http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic10[^],
http://msdn.microsoft.com/en-us/vstudio/jj131514.aspx[^].

[EDIT #1]

By the way, confusing naming is pretty usual. For example, the name System.Object is the name of the class, not object. For those who understand the essence of things, this is not really a problem. Also, the syntax allows to give a variable or a type's member the same name as its type; and this is even recommended. You need first to distinguish between types, instances (objects), variable, type's fields, properties (and other members), and then consider their roles.

[EDIT #2]

Please see also my past answers to related questions:
how to call keydown event on particular button click[^],
[Solved] How to add Event for C# Control[^],
A question about usercontrols, nested controls and encapsulation.[^],
WPF : How to Use Event in Custom Control[^].

And, more on anonymous methods and such non-trivial thing as closure:
What are Anonymous Types in C#?[^],
Looping for creating new thread give some problem[^],
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

—SA
 
Share this answer
 
v6
Comments
Espen Harlinn 18-Sep-12 10:03am    
Well done :-D
Sergey Alexandrovich Kryukov 18-Sep-12 14:02pm    
Thank you, Espen.
--SA
fjdiewornncalwe 18-Sep-12 14:08pm    
Fantastic. +5.
Sergey Alexandrovich Kryukov 18-Sep-12 14:10pm    
Thank you very much, Marcus.
--SA
Maciej Los 18-Sep-12 15:48pm    
Excellent! BIG 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900