Now with C# 7, we can return by ref with return ref
. From what I've gathered, references are 32 or 64 bits. Now, if I had a struct Coord
with a long X
and long Y
, that would be 128 bits, so it'd be easier return the coord, (as well as pass it) by reference to avoid copying the 128 bits.
On the other hand, if I were to try to return ref
a byte
, which is only 8 bits, the reference to it would be much larger than copying byte itself, right?
So, my main question: If the object we want to return can be returned by ref
(ie, not a local variable) and its size is larger than the size of a reference, should we return by ref
?
Edit: Quick code example
// This size is 128 bytes, which is 2 or 4x larger than the size of a reference
public struct Coord
{
public long X, Y;
}
private Coord myCoord;
// This will return the Coord by value, meaning copying the full 128 bytes
public Coord GetCoordValue() => myCoord;
// This will return the Coord by reference, meaning copying only 32 or 64 bytes
public ref readonly Coord GetCoordRef() => ref myCoord;
Also, this particular struct is very simple and it's already 2/4x smaller to return by ref
.
Edit 2: I made GetCoordRef()
readonly
so that the caller can't save and change the value of myCoord
, thereby preserving encapsulation, though I imagine this still wouldn't be smart to default to using it.
A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.
Functions in C++ can return a reference as it's returns a pointer. When function returns a reference it means it returns a implicit pointer.
It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.
Explanation: This is possible but not always, since the reference being returned may get destroyed with the return of method. This is an undesirable condition, hence it is not always possible to return references. But it is always possible if the referred element is not local to the method.
If the object we want to return can be returned by ref (ie, not a local variable) and its size is larger than the size of a reference, should we return by ref?
(1) No.
(2) For advanced users only: yes, in precisely those cases where your empirical, accurate profiling data indicates that making this change transforms your program from one that disappoints users into one that delights users.
Are there programs in the marketplace whose failure or success can be traced back to the couple nanoseconds difference of copying a couple extra bytes being traded for the couple nanoseconds of making a pointer indirection later? I'm not aware of any, but maybe you write programs whose users have very tight budgets for the number of nanoseconds they want your API to take.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With