Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / February 2005

Tip: Looking for answers? Try searching our database.

Passing Parameters to Functions Performance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Anthony Spiteri - 15 Feb 2005 06:38 GMT
Hi,

I have recently read that passing parameters by value
provides better performance then passing parameters by
reference.

Besides that I have also read that VB.NET does not
actually pass a reference to an object (when using ByRef)
but what it actually does is copy to value of the object
back and forth (since the same value has to be used).

Could someone please shed some light on this?

Thanks,
Mark
Jon Skeet [C# MVP] - 15 Feb 2005 07:28 GMT
> I have recently read that passing parameters by value
> provides better performance then passing parameters by
> reference.

Yes, although the difference is fairly slight. It's much more important
that you pick the appropriate semantics, frankly. Don't pass by
reference unless you really mean to.

> Besides that I have also read that VB.NET does not
> actually pass a reference to an object (when using ByRef)
> but what it actually does is copy to value of the object
> back and forth (since the same value has to be used).
>
> Could someone please shed some light on this?

It depends on whether the type in question is a reference type or a
value type, and in VB.NET's case, whether you're passing a property by
reference or a variable by reference. You can't pass a property by
reference in C# - but in VB.NET, passing a property by reference
basically creates a hidden variable which starts off with the current
value of the property, calls the method passing that variable by
reference, then sets the new value of the property to the new value of
the variable. This has somewhat different semantics to passing a
variable by reference, and is generally ugly IMO.

In no case is a copy of the object itself made (assuming the parameter
is a reference type to start with). Could you say where you read about
the copying?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mark Anthony Spiteri - 15 Feb 2005 08:19 GMT
I have read this at: http://www.dotnetextreme.com/articles/performance.asp

However based on your reply I am assuiming that the author was either
referring to just passing properties or else he mistakinly was using the two
interchangeably.

Going back to the orginal question, could you please clarify what you mean
by "It depends on whether the type in question is a reference type or a
value type" ?

> > I have recently read that passing parameters by value
> > provides better performance then passing parameters by
[quoted text clipped - 24 lines]
> is a reference type to start with). Could you say where you read about
> the copying?
Jon Skeet [C# MVP] - 15 Feb 2005 17:47 GMT
> I have read this at: http://www.dotnetextreme.com/articles/performance.asp
>
> However based on your reply I am assuiming that the author was either
> referring to just passing properties or else he mistakinly was using the two
> interchangeably.

Well, his language is pretty vague - it certainly *looks* to me like he
doesn't know what he's talking about, I'm afraid.

> Going back to the orginal question, could you please clarify what you mean
> by "It depends on whether the type in question is a reference type or a
> value type" ?

Well, the exact cost of passing a parameter depends on its size. For a
reference type, passing by value only requires copying the reference
itself, which is 4 bytes (on a 32-bit CLR). For a value type, all the
data in the value needs to be copied. When a value type is passed by
reference, however, only the address of that value is passed - there's
an indirection cost on access, but the actual passing is relatively
cheap.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.