Paul Laudeman

Sponsors

The Lounge

News

  • I'm test-driven!
    TestDriven.NET

    MSN Messenger: plaudeman at hotmail dot com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Passing parameters: ref, out, byref, byval - what does it all mean?

Today I came across a post that asked “Are reference types true?” The author was, I believe, trying to make the point that the behavior of reference types passed to methods may behave in a way you don't expect. (Click the link above to see the code sample in question.)

In this particular case, the expected output is 20, not 10. The value 10 would be true only if the valhold class was passed into method1 using the "ref" keyword. The default behavior of object passing in .NET is to pass a copy of the object in the case of value types or, in the case of reference types, to pass a copy of the object reference (or pointer, not the actual object reference itself). Since method1 in the example takes a valhold class as-is (by copying the reference to the reference), you can still manipulate the contents of the original object. However, if you replace the original object in the method body, then you are now dealing with a new object and any changes will not be made to the original object.

Here are a couple of good articles explaining how it all works:

Copying, Cloning, and Marshalling in .NET
http://www.ondotnet.com/pub/a/dotnet/2002/11/25/copying.html

C# Programmer's Reference: Passing Parameters
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfpassingmethodparameters.asp

UPDATED:

Carl and Rory from DotNetRocks Radio give you a entertaining overview from their VB.NET Master Class on objects in .NET - this link is great if you need a visual explanation of how it all works (especially useful for those of you with a prior VB background)
http://www.franklins.net/dotnetrocks/dnrvideos.aspx


Posted 11-07-2003 1:11 PM by paul.laudeman

[Advertisement]

Comments

JosephCooney wrote re: Passing parameters: ref, out, byref, byval - what does it all mean?
on 11-09-2003 11:05 AM
I'm still slightly amazed/scared when I talk to people who have been doing .NET for a while and see signs that they don't understand the difference between reference and value types. Thanks for the links, I will forward them on to others whenever I see signs that they don't "get it".
Paul Laudeman wrote re: Passing parameters: ref, out, byref, byval - what does it all mean?
on 11-09-2003 12:17 PM
Glad to help! I wonder if a lot of this confusion comes from the "RAD" focus of .NET over solid programming principles and understanding. It's all too easy to "drag and drop" your way to a "Rapid Application Disaster".
Vijay wrote re: Passing parameters: ref, out, byref, byval - what does it all mean?
on 11-09-2003 7:32 PM
Paul,

Thanks for the link. I understand that ref keyword can be used to get the value 10 as expected but my problem is that shouldn't the objects default behaviour be like that ? That is where i got really confused taking it further from C pointers into passing Object reference between methods in .NET now...

Mark Fenton wrote re: Passing parameters: ref, out, byref, byval - what does it all mean?
on 03-23-2004 2:25 AM
It should be noted that when passing classes you do not need to use the ref keyword if you want to change the object.

This is a serious flaw in my view - especially given that you cannot enforce a parameter to be const - i.e. when passing an object to a function you cannot be sure that the function doesn't change your object accidently.
Paul Laudeman wrote re: Passing parameters: ref, out, byref, byval - what does it all mean?
on 03-23-2004 3:02 AM
Agreed. Good point to be mindful of as you develop.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?