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() {
            return;
      }
}


... 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.

MiloDCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
As per MSDN article there is no difference between the two methods
http://msdn.microsoft.com/en-us/library/ms366768(VS.80).aspx
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MiloDCAuthor Commented:
OK, so it's necessary to do it the first way ("new") only in C# 1.0.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.