C# Error
CS8163 – Fields of static readonly field ‘{0}’ cannot be returned by writable reference
Reason for the Error & Solution
Fields of static readonly field cannot be returned by writable reference
Example
The following sample generates CS8163:
// CS8163.cs (12,14)
public class Test
{
public struct S1
{
public char x;
}
public static readonly S1 s2;
char Test2()
{
return s2.x;
}
}
To correct this error
To return the value of a static readonly
field, refactoring to return by value corrects this error:
public class Test
{
public struct S1
{
public char x;
}
public static readonly S1 s2;
char Test2()
{
return s2.x;
}
}