If we have two methods, one returning a variable by value, and the other by reference, which has the highest performance?
myObj.Method1(out var);
or
var = myObj.Method2();
I guess the first version is more efficient but, does that mean you should always build methods that return values by reference? Or is there any reason to return variables by value?
Thanks.
The performance difference will be immeasurably small or nonexistent.
You are incorrectly assuming that the two versions have different semantics.
For reference types, both methods will copy a reference exactly once.
For large value types, out
parameters can be faster, since you don't need a separate temporary local.
Always measure before jumping to conclusions!
Do not use out
parameters unless you need to return 2 values.