This may seem like a dumb question, or even subjective, but I’m not sure what’s the right way to do this.
I have a method that receives a class and alters it (in particular, it adds a new column to a
DataTable). I understand that, working with reference types, I can pass it by-value or by-reference, the behavior won’t change in this case.
In one hand, I don’t like adding more indirection than needed. If by-val works, I like to use it.
In the other hand, by-ref makes my intention clearer (Although the method’s name should be enough for this).
Which should I use, and why?
No, the behaviour does change when a reference type parameter is “by reference”. That means changes to the parameter itself will be seen by the caller.
If you make a parameter “by reference” when you don’t want that, you’re making your code less clear rather than more clear. It suggests that you will change the parameter, rather than just modifying the object that the parameter refers to. That will also restrict how your method can be called, as the argument would have to be an l-value. Not a good idea.
See my article on parameter passing for more information.