Return by value or link?

If we have two methods, one returns the variable by value, and the other by reference, which has the highest performance?

myObj.Method1(out var);

or

var = myObj.Method2();

I assume the first version is more efficient, but does this mean that you should always create methods that return values ​​by reference? Or is there some reason to return variables by value?

Thank.

+3
source share
3 answers

The difference in performance will be immeasurably small or nonexistent.

You mistakenly believe that the two versions have different semantics.
For reference types, both methods will copy the link exactly once.

out , .
, !

out, 2 .

+6

, .

, , , . , .

+4

() , .

(structs) , , out .

+1

Source: https://habr.com/ru/post/1788859/


All Articles