471,317 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

A question about Events in C#

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

Dec 28 '05 #1
3 1137
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.

Dec 28 '05 #2
Amit <am********@yahoo.com> wrote:
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/t...ckchoice.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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #3
Bruce Wood <br*******@canada.com> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
1 post views Thread by Steve | last post: by
4 posts views Thread by news.microsoft.com | last post: by
1 post views Thread by Christoph Boget | last post: by
6 posts views Thread by Flare | last post: by
1 post views Thread by e-mid | last post: by
1 post views Thread by Tom Rahav | last post: by
1 post views Thread by swethak | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.