A question about Events in C#

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

Hi

I have a question about events in C#.

In C#, we declare an event like this.

public delegate void EventHandlerA()
public class A
{
public event EventHandlerA eventObject = null;

private void SomeMethod()
{
if (eventObject != null)
{
eventObject();
}
}
}

class ClientOfClassA
{
ClassA objClassA = new ClassA();
objClassA.eventObject += new EventHandlerA(someFunctionOfClient)
}

My questiion is
When the user created an instance of classA, the classA.eventObject is
set to null. Immidiately after that the user hooks to the event by
executing += operator. If the object is null, why an exception is NOT
thrown when we execute += operator on a null object.

Best regards
Amit
 
Because in this case += doesn't really mean "add a value to"... it
means "add a new event handler to". The only reason I can imagine why
the C# decided to make a delegate with no client handlers "null"
instead of an empty list of event handlers is to save a little bit of
memory for events that have no subscribers. That little bit of memory,
over and over again (and the code to initialize it) probably saves
quite a bit in a good-sized program.

So, in the end, it's just a convention. Somewhere under the covers the
compiler (or the CLR) knows that when you say += for an event that has
the value null, it means to allocate a list for handlers and add the
given handler, just as when you say -= to remove the last event handler
from an event, the compiler (or the CLR) knows to remove the last
handler and set the event delegate to null.
 
Amit said:
I have a question about events in C#.

In C#, we declare an event like this.

public delegate void EventHandlerA()
public class A
{
public event EventHandlerA eventObject = null;

private void SomeMethod()
{
if (eventObject != null)
{
eventObject();
}
}
}

Well, you may do - I don't (in any code which needs to be threadsafe).

See http://www.pobox.com/~skeet/csharp/threads/lockchoice.shtml
class ClientOfClassA
{
ClassA objClassA = new ClassA();
objClassA.eventObject += new EventHandlerA(someFunctionOfClient)
}

My questiion is
When the user created an instance of classA, the classA.eventObject is
set to null. Immidiately after that the user hooks to the event by
executing += operator. If the object is null, why an exception is NOT
thrown when we execute += operator on a null object.

Because the auto-generated "add" code (which is actually invoked when
you use += on an event) uses Delegate.Combine, which is a static method
which can take null parameters.
 
Bruce Wood said:
Because in this case += doesn't really mean "add a value to"... it
means "add a new event handler to". The only reason I can imagine why
the C# decided to make a delegate with no client handlers "null"
instead of an empty list of event handlers is to save a little bit of
memory for events that have no subscribers. That little bit of memory,
over and over again (and the code to initialize it) probably saves
quite a bit in a good-sized program.

It's not really C#, it's the CLR. I can see why they did this -
consider default construction of an instance of a type. It's usually
just a case of setting all bits to zero, then running a constructor. It
would be a pain (in many places, I suspect) if you had to ensure that
you *always* ran an extra bit of code to change the value of delegate
fields was initialized to a non-null value. It's certainly a pain in
the neck though. Of course, if you're using your own event add/remove
code, you can always set the initial value of the event delegate
yourself.

<snip>
 
Back
Top