Errore del compilatore CS1656

Impossibile assegnare a 'variabile' perché è un 'tipo di variabile di sola lettura'

Questo errore si verifica quando un'assegnazione a una variabile viene eseguita in un contesto di sola lettura. I contesti di sola lettura includono le variabili di iterazione foreach , le variabili using e le variabili fixed. Per risolvere l'errore, evitare le assegnazioni a una variabile di istruzione in blocchi using, istruzioni foreach e istruzioni fixed.

Esempio 1

L'esempio seguente genera l'errore CS1656 poiché viene tentata la sostituzione di elementi completi di una raccolta all'interno di un ciclo foreach. Un modo per risolvere l'errore consiste nel sostituire il ciclo foreach con un ciclo for. Un altro modo, non illustrato qui, consiste nel modificare i membri dell'elemento esistente. Ciò è possibile con le classi, ma non con gli struct.

using System;  
using System.Collections;  
using System.Collections.Generic;  
using System.Text;  
  
namespace CS1656_2  
{  
  
    class Book  
    {  
        public string Title;  
        public string Author;  
        public double Price;  
        public Book(string t, string a, double p)  
        {  
            Title=t;  
            Author=a;  
            Price=p;  
  
        }  
    }  
  
    class Program  
    {  
        private List<Book> list;  
        static void Main(string[] args)  
        {  
            Program prog = new Program();  
            prog.list = new List<Book>();  
            prog.list.Add(new Book ("The C# Programming Language",  
                                    "Hejlsberg, Wiltamuth, Golde",  
                                     29.95));  
            prog.list.Add(new Book ("The C++ Programming Language",  
                                    "Stroustrup",  
                                     29.95));  
            prog.list.Add(new Book ("The C Programming Language",  
                                    "Kernighan, Ritchie",  
                                    29.95));  
            foreach(Book b in prog.list)  
            {  
                // Cannot modify an entire element in a foreach loop
                // even with reference types.  
                // Use a for or while loop instead  
                if (b.Title == "The C Programming Language")  
                    // Cannot assign to 'b' because it is a 'foreach
                    // iteration variable'  
                    b = new Book("Programming Windows, 5th Ed.", "Petzold", 29.95); //CS1656  
            }  
  
            //With a for loop you can modify elements  
            //for(int x = 0; x < prog.list.Count; x++)  
            //{  
            //    if(prog.list[x].Title== "The C Programming Language")  
            //        prog.list[x] = new Book("Programming Windows, 5th Ed.", "Petzold", 29.95);  
            //}  
            //foreach(Book b in prog.list)  
            //    Console.WriteLine(b.Title);  
  
        }  
    }  
}  

Esempio 2

L'esempio seguente dimostra come l'errore CS1656 possa essere generato in altri contesti, diversi da un ciclo foreach:

// CS1656.cs  
// compile with: /unsafe  
using System;  
  
class C : IDisposable  
{  
    public void Dispose() { }  
}  
  
class CMain  
{  
    unsafe public static void Main()  
    {  
        using (C c = new C())  
        {  
            // Cannot assign to 'c' because it is a 'using variable'  
            c = new C(); // CS1656  
        }  
  
        int[] ary = new int[] { 1, 2, 3, 4 };  
        fixed (int* p = ary)  
        {  
            // Cannot assign to 'p' because it is a 'fixed variable'  
            p = null; // CS1656  
        }  
    }  
}