EventArgs pattern

E

Eric Cadwell

What advantages do I reap by following the EventArgs pattern in defining
custom event delegates?

For example...

public delegate void CustomEventHandler(object sender, CustomEventArgs e);
public class CustomEventArgs : System.EventArgs
{
int index = 0;
public CustomEventArgs(int Index)
{
index = Index;
}
public int Index
{
get { return index; }
}
}

....instead of simply using...

public delegate void CustomEventHandler(int Index);


It's so much more work. There's a lot of boilerplate in writing events and
controls.

Thanks,
Eric
 
N

Nicholas Paldino [.NET/C# MVP]

Eric,

The overall reason is that it is quite simply the pattern that was
established. While this doesn't sound like the best reason in the world, it
is the way it is, and it makes everything easier because its implementation
is just short of universal.

However, I can say that there is one benefit to this pattern. Because a
method can be registered to handle multiple event notifications, the sender
parameter becomes very, very important. It allows for more reusable code.

Hope this helps.
 
G

Guest

Because then we have a uniform way to specify events.

Btw you should have youre field set to readonly unless you are doing
something like cancellable events then its writable

If you dont need custom event args you can just use the EventArgs base.

Why dont you just make a template of the event or a wizard to generate the
..cs file then its a simple matter of right click -> Add new Event (just like
add new class) in the IDE context menu (theres an idea :D how bout that in
the next IDE?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top