Passing parameters by Ref or Out

Posts   
 
    
mattsmith321 avatar
Posts: 146
Joined: 04-Oct-2004
# Posted on: 27-Jan-2005 03:29:54   

Does anyone have any real-world advice to share regarding passing parameters either by ref or out?

I notice that when I call: DataAccessAdapter.SaveEntity(myEntity, true) that myEntity contains the updated ID without having the call be myEntity = DataAccessAdapter.SaveEntity(myEntity, true)

So, it appears as if the entity is being passed in by either ref or out.

However, where I get confused is that the doco for ref (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfref.asp) and out (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfout.asp) state that "the argument must explicitly be passed to the method as an out/ref argument". But I don't see that happening in what Frans is doing.

Please enlighten me.

cmartinbot
User
Posts: 147
Joined: 08-Jan-2004
# Posted on: 27-Jan-2005 05:54:36   

You should read about value types vs. reference types in dotnet. Here is a good page I found with google: http://www.knowdotnet.com/articles/referencetypes2.html

mattsmith321 avatar
Posts: 146
Joined: 04-Oct-2004
# Posted on: 27-Jan-2005 17:19:24   

cmartinbot wrote:

You should read about value types vs. reference types in dotnet. Here is a good page I found with google: http://www.knowdotnet.com/articles/referencetypes2.html

Yeah, I've done a fair amount of reading. This article was really good: Parameter passing in C# (http://www.yoda.arachsys.com/csharp/parameters.html).

Initially I was confused because I was thinking that you could only pass by reference when explicitly using the ref or out notations and I was confused how my entities were changing without the notations. However, in the article above, this section seems to apply to the LLBL Gen entities:

Remember though that the value of a reference type variable is the reference - if two reference type variables refer to the same object, then changes to the data in that object will be seen via both variables.

In their given example for this section:

void Foo (StringBuilder x)
{
    x.Append (" world");
}

StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (y);
Console.WriteLine (y);

the output is:

hello world

which appears to be the way the LLBL entities work.

Knowing more about it now, and knowing that there are probably lots of other people not aware of the subtle differences, it seems that more people are lucky that they don't run into 'mysterious' issues on why their objects keep changing on them.

Thanks for listening!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39752
Joined: 17-Aug-2003
# Posted on: 27-Jan-2005 18:28:55   

I find the difference between passing an object reference normally and using ref also pretty confusing, as in general it doesn't matter: ref is not needed for object references. (pointer to pointer or just pointer). There is a difference of course, but utilizing that difference (passing a ref object parameter using ref, then set that parameter to null) is asking for severe trouble (IMHO)

I use ref on parameters a method will modify, even though the type is a reference type. Just for clarification.

The article of Jon Skeet you link to is very good for understanding simple_smile

Frans Bouma | Lead developer LLBLGen Pro