Why wouldn't this work?
I create the struct and want to return it by ref.
public readonly struct AuditResult
{
public readonly bool AcceptChanges;
public readonly string Reason;
public AuditResult(bool acceptChanges, string reason)
{
AcceptChanges = acceptChanges;
Reason = reason;
}
public static ref AuditResult AcceptAuditResult()
{
var auditResult = (new AuditResult(true, string.Empty));
ref AuditResult res = ref auditResult;
return ref res;
}
}
With this error happens:
CS8157 Cannot return 'res' by reference because it was initialized to a value that cannot be returned by reference
My variable is a ref
in this case?
The C# documentation states that:
The return value must have a lifetime that extends beyond the execution of the method. In other words, it cannot be a local variable in the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to the method. Attempting to return a local variable generates compiler error CS8168, "Cannot return local 'obj' by reference because it is not a ref local."
In order to use the return ref
keyword, you will need to return an object that is not a local variable, as a local variable will go out of scope and be garbage collected. Instead, consider returning a reference to a member variable in your class/struct, as opposed to res
. Also, consider whether you need to return the value by reference at all - if you are not accessing it somewhere else internally, there is no need to pass it by reference.
Note, however, that you can use the ref
keyword locally to create aliases for local variable names, like so:
Foo a = new Foo();
ref Foo b = ref a;
Here, modifying b
will also modify a
. With this syntax, though, you can't pass local references outside of the current method scope.