Good afternoon, there is such a code:

class Program { static void Main(string[] args) { const string a = ""; Console.WriteLine("Представьтесь"); a = Console.ReadLine(); } } 

I want to read the text in the variable a. And it gives an error - the left side of the assignment expression must be a variable property or an indexer. What is the problem?

  • 3
    /*const*/ string a = ""; - Igor

2 answers 2

Your variable "a" is a constant, because is the keyword const. Its value cannot be changed. Remove the keyword const and everything will work. You can rewrite your code with this:

 Console.WriteLine("Представьтесь"); string a = Console.ReadLine(); 

The fact is that the constant after initialization of the value cannot change until the end of the program.

    You declared a variable as a constant.

    A constant is an identifier whose value never changes.

    Since the value of a constant is embedded directly into the code, memory for constants is not allocated at run time and therefore you cannot change its value.

    In order for a variable to be written and read, declare it as follows:

     string a = String.Empty;