+ 1
What is the point of passing by ref?
Why not just make the method return a value?
4 Respostas
+ 9
consider situation that you change multiple variables in function, you can return just one variable from function, but can send multiple variables to function by reference
+ 9
If your input to a function/method is a big object (an array with 10000 entries for example) and you just want to modify a few elements, there is no need to create a copy of the whole object. But that's precisely what happens, if it's passed by value.
Passing by reference allows you to modify the object itself without needing to copy it.
+ 5
Copies use more resources, including CPU time, stack space and memory.
It's the difference between referring to a person by name and stopping to scan and make a complete clone of them every time they come up.
Are there use cases in each scenario? (depends on what you need)