Actually I am doing a list as a reference parameter as follows:
public static List ListMethod(List result)
I saw some people doing in this way too:
public static void ListMethod(ref List result)
If I’m not wrong, “my” method also takes the list as reference parameter, and you should be able to use it just the same way as “other” does in his method.
But it seems more “clean” to me that you enter a parameter, do something with it and return it in the methods return value.
Any good arguments for or against one method or the other?
It’s likely that you don’t need to use
ref– but there is a difference.Usually when I see people using
reffor reference type parameters, it’s because they don’t understand how parameter passing works. But if your method has something like this:then in the first case the caller won’t see the change, whereas in the second case the caller’s variable will be changed to refer to the new object.
See my article on parameter passing for a lot more detail.