Link to home
Start Free TrialLog in
Avatar of devo00
devo00Flag for United States of America

asked on

When to use pass-by-value vs. pass-by-reference?

I have a co-worker that uses pass by reference on most parameters for most method calls (between DAL / BLL for example, SqlConnection object to a common method that creates an open a connection, or even among methods in the same class).

Does this provide an advantage or is this incorrect usage? (i.e. When should I use either one method vs. the other and why?)

(I do know usage with strings does not work with ref params.)

Thanks
ASKER CERTIFIED SOLUTION
Avatar of jamesrh
jamesrh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
Pass by ref and value aren't as big of an issue in .NET as they are in C/C++. I believe everything in .NET is passed by value--value types create copies of the data and reference types create copies of the pointer. When passing an initialized ref type to a function, you have access to the objects members for modification because you are working with a pointer to the original data. Where you run into trouble is if you were to pass a ref type to a function that at some point during execution called "new". The new memory would be allocated, but the COPY of the pointer would hold the reference--not the original pointer. In order to call new within a function and have the original pointer maintain a reference to the new data, you have to use either the "ref" or "out" keywords, which have the effect of pass by ref.
Passing parameter by ref makes sense only for value types.
Reason: Value types stored on the thread's stack. When value type (Int32, Double, any struct, etc) passed to the method, value type object copy made and passed to method. You can see this by changing parameter inside method - all changes will not affect original object. Reference types are passed by default by reference.

So, there are two ways I see to use ref:
1) you have some big value type object, you can pass it by ref to speed up method call and save stack memory
2) you want to change original value type object inside called method

I don't like both :)
Avatar of shadow77
shadow77

If you make the assumption that the language has been well-designed, and in particular that care has been taken in designing the parameter and result passing mechanisms, they you should trust the designers and use the default mechanism unless you have a good reason to do otherwise.  There is thus an important clue in the fact that you must explicitly specify ref if you want to pass by ref and you must do so in both the method declaration and the method call.

Suppose you have something like
void f(c) { ... c += 1; ...}
void g() { int k = 5; ... f(k); ... }
If you really want f to change k, then you would pass k by reference.

Because structs are value types, they are copied when they are passed by value (the default).  If you had a large struct, you would see some performance benefit if you passed it by reference, especially if you did so often. However, you would also lose the safety granted by passing by value because and method you called would be able to modify the original copy of your struct.

In summary, pass by value unless you have a good reason to do otherwise.  "Always" is not a good reason; it's lazy.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devo00

ASKER

Fantastic answers guys, sorry, I just couldn't pick one, the first put it nicely in layman's terms (which I needed), and the latter will fill me in on what the heck is going on.