Tuesday, May 27, 2008

Event : Public or Private ??

Event declaration in a C# class exhibits strange property as far as its access modifiers are concerned.Declare your class with a "public" access modifier attached to your event.

public class TestClass
{
public event EventHandler myTestEvent;
public TestClass()
{

}
}

Now build your application and open up in ildasm and observe the access modifier associated with your event. Strangely, it is "private".

.field private class [mscorlib]System.EventHandler myTestEvent

This somewhat explains why we are able to "catch" a event from another class but not raise another class event from your class even if its public.
I should thank my friend Hari for bring this to my notice.