更新:2007 年 11 月
错误消息
无法修改“variable”的成员,因为它是一个“read-only variable type”当您试图修改一个只读变量的成员时发生此错误,因为它位于特殊的结构中。
此错误发生的一个常见区域位于
示例
下面的示例生成错误 CS1654,因为 Book 为 struct。若要修复此错误,请将 struct 更改为
复制代码 | |
---|---|
using System.Collections.Generic; using System.Text; namespace CS1654 { struct 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 { List<Book> list; static void Main(string[] args) { //Use a collection initializer to initialize the list 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) { //Compile error if Book is a struct //Make Book a class to modify its members b.Price +=9.95; // CS1654 } } } } |