I have seen far too many examples where VB.NET code does not check to see if there are any listeners prior to raising the event. The C# examples seem to always check. For example, take a look at the MSDN code example for INotifyPropertyChanged. The two snippets below are from this site.
Raising an event in C# taken from MSDN
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
Raising an event in VB.NET taken from MSDN
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
The above two snippets are fairly typical. The C# example checks to see if there are any listeners and only if there are does it raise the event. Contrast that to the VB.NET code where the RaiseEvent is called regardless of whether there are any listeners to the event. Both code examples work fine. It would seem in VB.NET the RaiseEvent call will not throw a null exception when there are no listeners.
How do we check for listeners in the VB.NET world. The obvious would be to add If PropertyChanged IsNot Nothing Then but you'll quickly find referencing PropertyChanged in this manner will not compile. The proper way is shown in the listing below.
Checking for subscribers in VB.NET
6 Public Event PropertyChanged( _
7 ByVal sender As Object, _
8 ByVal e As System.ComponentModel.PropertyChangedEventArgs) _
9 Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
10
11 Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
12 If PropertyChangedEvent IsNot Nothing Then
13 RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
14 End If
15 End Sub
The trick is to add the Event suffix to the event name, in our case PropertyChangedEvent, and check to see whether it has a value. So why don't we not see this code pattern more often. Firstly, it doesn't seem to be well documented and secondly this reference variable is not reported by intellisense.
The question is it good practise to check whether there are any listeners? In C# it is a given, otherwise, a null reference exception will be throw. In VB.NET no exception will be thrown and the call to RaiseEvents will not throw an exception when there are no listeners. But which is faster? I wrote a simple application to check the amount of time it takes to blinding call RaiseEvent versus the check for the existence of the listener. Checking the listeners prior to calling RaiseEvent is about 1.75 faster than just calling RaiseEvent blindly. It should be noted that even though the check is 1.75 times faster, the actual amount of time to call RaiseEvent is extremely small. A call to RaiseEvent with no listeners takes approximately 0.000000008 seconds.
In the end, checking for listeners, in VB.NET, will in all likelihood not impact the performance of the code.
Guess the movie
Why am I such a misfit? I am not just a nitwit. You can't fire me, I quit. Seems I don't fit in.