473,549 Members | 3,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1663
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.que ensu.ca> wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.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
1657
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 unregister on dispose. Before my application exits, I'd like to do an 'assert' to assure myself that there are no cases in which I've forgotten to...
8
5530
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 should work, too. Or is my only solution to loop through each one and use ..addEventListener? And then trap for any DOM modifiactions in case new...
2
3902
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 well, so at times it is tempting just to port parts of it over mostly as-is. In fact, one MSDN article I read suggested using straight HTML wherever...
7
15978
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 events all go to the same handler, but I want one of them to go to a separate handler also. This is done in Java, IIRC.
0
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.