It is a given that private members should be wrapped with public accessors for proper encapsulation of class data. But it is common to see people publicly declaring events, such as with public event EventHandler SomeEventExposingHimself;. Essentially, this is the same as exposing a private member to the world without accessors, such as with public string SomeStringThatYouShouldNotHaveAccessToDirectly;. Any subscriber listening for the event is now tied directly to the actual class member. C# makes it easy to provide accessors for your events, just as it does for class members, with the keywords add and remove. The following is an example of an event properly wrapped with public accessors:
public
event EventHandler EventModestlyExposed {
add {
someEventNotWantingToExposeHimself += value;
}
remove {
someEventNotWantingToExposeHimself -= value;
}
}
private
event EventHandler someEventNotWantingToExposeHimself;
Billy