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.
A readonly field cannot be returned by writable reference
Example
The following sample generates CS8160:
// CS8160.cs (8,20)
class Program
{
readonly int i = 0;
ref int M()
{
return ref i;
}
}
To correct this error
To return a readonly field, refactor to return by value:
class Program
{
readonly int i = 0;
int M()
{
return i;
}
}
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.