Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Cannot return by reference because it was initialized to a value that cannot be returned by reference
Example
The following sample generates CS8157:
// CS8157.cs (8,21)
class C
{
ref int M()
{
int x = 0;
ref int rx = ref x;
return ref (rx = ref (new int[1])[0]);
}
}
To correct this error
To return a value that cannot be returned by reference, refactoring to return by value corrects this error:
class C
{
int M()
{
int x = 0;
ref int rx = ref x;
return rx = ref (new int[1])[0];
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.