Home > C# > C# – Event Subscription – with new keyword vs without

C# – Event Subscription – with new keyword vs without

Per MSDN here, Event Subscription with new keyword and without are the same.
The one with new was introduced in C# 1.0. It then gets simplified in C# 2.0.

C#1.0: pub.OnCustomEvent += new CustomEventHandler(HandleCustomEvent);

C#2.0: pub.OnCustomEvent += HandleCustomEvent;

 

Excerpt from MSDN:

2. Use the addition assignment operator (+=) to attach your event handler to the event. In the following example, assume that an object named publisher has an event named RaiseCustomEvent. Note that the subscriber class needs a reference to the publisher class in order to subscribe to its events.

publisher.RaiseCustomEvent += HandleCustomEvent;

Note that the above syntax is new in C# 2.0. It is exactly equivalent to the C# 1.0 syntax in which the encapsulating delegate must be explicitly created using the new keyword:

publisher.RaiseCustomEvent += new CustomEventHandler(HandleCustomEvent);

 

End excerpt

Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment