473,324 Members | 2,248 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,324 software developers and data experts.

Event handlers and memory usage

Hi all, I'm inquiring about the mechanism that is used to implement
event handlers in .NET (not really relevant, but I'm using C#).
Anyway, I've noticed that I can pass an object in an event argument and
if the receiver (event handler) of that event changes any of the
properties in that object, then the original object I passed is
changed. I.E. it seems as if events are passed by reference and not by
value. This certainly frees me up from trying to optimize memory usage
when I'm passing objects (or actually their references) through event
arguments.

I was thinking that events were serialized - but it seems that isn't
the case. Since if they were being serialized then any changes made to
the object by the event handler shouldn't affect the original that was
passed as an argument.

Hope this is clear - the reason I'm asking this is because I have a
rather large object that I'm passing as an event argument and I was
going through the code in an attempt to optimize only to realize that
there doesn't appear to be any performance benefit to passing a unique
identifier string for that object and then find that object in my
database or whether I just pass this entire massive object.

Thanks,
Novice

Mar 2 '06 #1
2 1648
All reference types are passed by reference. What this means is that a
managed pointer to the object is passed. Passing by value passes a copy of
an object. Primitives and value types are passed by value.

Think of it this way: If I mail you a copy of my house, I mail an entire
house to you. But if I mail you the address of my house, I can fit it on a
single sheet of paper. And if you enter the copy of my house I won't see
you. But if you come to the address of my house and enter it, I can see you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
<6t**@qlink.queensu.ca> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Hi all, I'm inquiring about the mechanism that is used to implement
event handlers in .NET (not really relevant, but I'm using C#).
Anyway, I've noticed that I can pass an object in an event argument and
if the receiver (event handler) of that event changes any of the
properties in that object, then the original object I passed is
changed. I.E. it seems as if events are passed by reference and not by
value. This certainly frees me up from trying to optimize memory usage
when I'm passing objects (or actually their references) through event
arguments.

I was thinking that events were serialized - but it seems that isn't
the case. Since if they were being serialized then any changes made to
the object by the event handler shouldn't affect the original that was
passed as an argument.

Hope this is clear - the reason I'm asking this is because I have a
rather large object that I'm passing as an event argument and I was
going through the code in an attempt to optimize only to realize that
there doesn't appear to be any performance benefit to passing a unique
identifier string for that object and then find that object in my
database or whether I just pass this entire massive object.

Thanks,
Novice

Mar 2 '06 #2
heh heh... I understand the difference between passing by copy and
value... but good analogy none the less...

I just thought events were being serialized... and that any argument
handed to them (be they primitive data type or not) were copied/cloned.

I guess I'll have to look elsewhere for optimizations - i.e. no sense
in reconstructing the object from the database, if I'm just passing a
reference to it.

Thanks,
Novice

Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: The One We Call 'Dave' | last post by:
Hi, Various custom controls in my WinForms application register for the 'Application.Idle' event on load, and unregister on dispose. To avoid memory leaks, it's essential that I remember to...
8
by: Csaba Gabor | last post by:
Is there any way in Mozilla/Firefox to add an event listener to all textarea elements? Something along the lines of HTMLTextAreaElement.prototype.onkeydown = function() {alert('mom');} only it...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
7
by: Dom | last post by:
In CSharp, can you have multiple handlers for a single event? For example, for a textbox TextChanged event, can I have two separate handlers called up? I have 4 text boxes, and the TextChanged...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.