• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Adding to an event with or without "new."

Given the following code:

public delegate void MyDelegate();
class MyClass {
      public event MyDelegate MyEvent;
      public void MyFunction() {

... In most online articles that I've read that explain how events work in C#, the author instructs the programmer to add MyFunction to MyEvent using the following syntax:

MyEvent += new MyDelegate(MyFunction);

However, I have ALWAYS been able to just do this:

MyEvent += MyFunction;

Can someone please explain to me the difference between the two, and why I should ever want to use the first method?

I prefer the second method because it's more concise, and because it looks syntactically the same as the method for *removing* a function from an event, i.e. MyEvent -= MyFunction.

1 Solution
Anurag ThakurTechnical ManagerCommented:
As per MSDN article there is no difference between the two methods
And the shortcut that you have been using has been added in the 2.0 framework

The only argument that i will be giving for using the syntax with new is that it will indicate which delegate i am referencing to (more readable).
I will prefer readability over lesser code as in longer run for code maintenance readable code is better than shorter code
MiloDCAuthor Commented:
OK, so it's necessary to do it the first way ("new") only in C# 1.0.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now