S
Smithers
Say we have private List<Thing>MethodA() that calls MethodB.
As MethodA() executes, it adds items to a List<Thing> instance that is
ultimately returned to its caller.
MethodB() also accumulates Thing items as it does its work, and adds them to
a List<Thing>.
At the end of the day, MethodA needs to return a List<Thing> that includes
the Thing items accumulated from both MethodA() and MethodB().
There are at least a couple of ways to make this happen...
Alternative #1 -- MethodA passes its List<Thing> instance to MethodB by
reference ('ref').
Alternative #2 -- Method B returns a List<Thing> that MethodA then
..AddRanges to its own List<Thing>.
Question:
Which of the above alternatives would be considered as "better" in a general
sense, for this sort of scenario, and why?
Is passing by 'ref' generally frowned upon in cases where it is not
necessary?
Thanks for your consideration.
As MethodA() executes, it adds items to a List<Thing> instance that is
ultimately returned to its caller.
MethodB() also accumulates Thing items as it does its work, and adds them to
a List<Thing>.
At the end of the day, MethodA needs to return a List<Thing> that includes
the Thing items accumulated from both MethodA() and MethodB().
There are at least a couple of ways to make this happen...
Alternative #1 -- MethodA passes its List<Thing> instance to MethodB by
reference ('ref').
Alternative #2 -- Method B returns a List<Thing> that MethodA then
..AddRanges to its own List<Thing>.
Question:
Which of the above alternatives would be considered as "better" in a general
sense, for this sort of scenario, and why?
Is passing by 'ref' generally frowned upon in cases where it is not
necessary?
Thanks for your consideration.