C# Error
CS8160 – A readonly field cannot be returned by writable reference
Reason for the Error & Solution
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, refactoring to return by value corrects this error:
class Program
{
readonly int i = 0;
int M()
{
return i;
}
}