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 static readonly field cannot be returned by writable reference
Example
The following sample generates CS8161:
// CS8161.cs (12,14)
public class Test
{
public struct S1
{
public char x;
}
public static readonly char s1;
ref char Test2()
{
return ref s1;
}
}
To correct this error
To return the value of a static readonly field, refactor to return by value:
public class Test
{
public struct S1
{
public char x;
}
public static readonly char s1;
char Test2()
{
return s1;
}
}
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.