HomeCSharpC# Error CS1650 – Fields of static readonly field ‘{0}’ cannot be assigned to (except in a static constructor or a variable initializer)

C# Error CS1650 – Fields of static readonly field ‘{0}’ cannot be assigned to (except in a static constructor or a variable initializer)

C# Error

CS1650 – Fields of static readonly field ‘{0}’ cannot be assigned to (except in a static constructor or a variable initializer)

Reason for the Error & Solution

Fields of static readonly field ‘identifier’ cannot be assigned to (except in a static constructor or a variable initializer)

This error occurs when you attempt to modify a member of a field which is readonly and static where it is not allowed to be modified. To resolve this error, limit assignments to readonly fields to the constructor or variable initializer, or remove the readonly keyword from the declaration of the field.

// CS1650.cs  
public struct Inner  
{  
    public int i;  
}  
  
class Outer  
{  
    public static readonly Inner inner = new Inner();  
}  
  
class D  
{  
    static void Main()  
    {  
        Outer.inner.i = 1;  // CS1650  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...