views:

964

answers:

5

Hello everyone,

Suppose I have the following code snippets, i.e. goo call foo, DataTable created by goo and foo will only call Rows.Add. Then goo will use the updated data of the DataTable instance from foo.

My question is, in my scenario, any differences or benefits compared of using with and using without ref parameter?

I am using C# + .Net 3.5 + VSTS2008.

    void foo (ref DataTable dt)
    {
        // call Row.Add on dt
    }

    void goo()
    {
        DataTable dt1 = ...; // create a new instance of DataTable here
        foo (ref dt1);
        // read updated content of DataTable by foo here
    }

thanks in advance, George

A: 

The only difference / benefit I see is that the code would be more clear if you didn't pass it by reference.

klabranche
Thanks! 1. from function perspective, they are of the same function in my scenario? 2. why do you think not using ref is clearer?
George2
The other replies have further stated why. You should only use by reference if you need to and in this case you do not. :)
klabranche
Thanks, klabranche!
George2
+2  A: 

In that case, there is absolutely zero difference between using a ref or a non-ref parameter. The only thing a ref param will allow you to do in this situation is "dt=new DataTable()" and have it affect the main function.

It is better to only use a ref param when you need it, otherwise it creates messier code.

Erich
"The only thing a ref param will allow you to do in this situation is "dt=new DataTable()" and have it affect the main function." -- I do not understand what do you mean "allow you to do" and "affect the main function". Any more descriptions?
George2
Ok, so: when you pass a parameter, it does 1 of 2 things, it passes the actual VALUE(a copy) of the parameter (non-ref), or it passes a pointer to the variable.In a primitive type (int, float, etc), it makes a huge difference, because altering the ref parameter in a function alters its value in the caller as well.
Erich
With object-types, you already are using a pointer, so any values changed ina non-ref context count. However, because you have a copy of the pointer (not the same one), re-assigning it wont affect the calling function. Using the ref-param, an assignment will actually change the value in the calling function.In this case, if Foo did dt=new DataTable(), the value of dt1 in Goo would be that new datatable. Without the ref, this isn't the case.
Erich
+3  A: 

You only use ref if you intend to change what dt points to. Eg., if you wanted to replace it with a different DataTable (or, a type deriving from DataTable).

In this case, you are just using dt, so there's no reason to use ref. Since DataTable is a class, any changes made to it will be visible outside of foo.

Mark Brackett
Cool, your reply is very clear.
George2
+3  A: 

You don't need the ref parameter unless you intend on getting a different object back. The DataTable is a reference object so what you are really passing is a pointer to a pointer. If all you intend is for the called function to make changes to your instance you don't need ref.

jlembke
Cool, your reply is very clear.
George2
A: 

Others have answered already, but if you want more information you might want to read my article on parameter passing in C#.

(Everyone is right - if you just want to change the data in the object, you're fine passing the argument by value. If you want to change which object the caller's variable refers to, then you pass by reference.)

Jon Skeet
@Jon, "if you just want to change the data in the object, you're fine passing the argument **by reference**" Shouldn't that be "by value"?
Ash
Oops, thanks, fixed :)
Jon Skeet