473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1172
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Steve | last post by:
I've run in to a problem with a query I'm trying to write. I have attached a sample SQL script at the end of this post to show an overview of what I'm working with. I want to be able to use...
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
4
by: news.microsoft.com | last post by:
In one of my books called "Mastering C#" there is a statement that reads "All event handler delegates must return void and accept two parameters. The first parameter is an object, and it represents...
1
by: Christoph Boget | last post by:
Where can I find *comprehensive* information about events? I've been all through Microsofts developer pages, through the VS.NET help but I can't find comprehensive, details information about them....
6
by: Flare | last post by:
Hi i have a qusstion about events and delegates. Especially the precis role of the Event. Eg. We have a class wich want to fire events so we declare: public delegate void...
1
by: e-mid | last post by:
i registered two events with a button. when i press it , how can i know which is fired first? or is there a way to make one of them get fired before the other one? in fact i made a experiment...
1
by: Tom Rahav | last post by:
Hi, I use VB .NET 2003 and I want to create a new UserControl that will function as a "smart" combobox that handles few events automatically (such as auto-complete, etc.) in addition to the...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.