Compiler Error CS0191
Property or indexer 'name' cannot be assigned to -- it is read only
A readonly field can only take an assignment in a constructor or at declaration. For more information, see Constructors.
CS0191 also results if the readonly
field is static and the constructor is not marked static
.
Example
The following sample generates CS0191.
// CS0191.cs
class MyClass
{
public readonly int TestInt = 6; // OK to assign to readonly field in declaration
MyClass()
{
TestInt = 11; // OK to assign to readonly field in constructor
}
public void TestReadOnly()
{
TestInt = 19; // CS0191
}
public static void Main()
{
}
}
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.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for