'변수'의 멤버는 '읽기 전용 변수 형식'이므로 수정할 수 없습니다.
이 오류는 특수 구문에 있으므로 읽기 전용인 변수의 멤버를 수정하려고 할 때 발생합니다.
이 문제가 발생하는 한 가지 공통 영역은 foreach 루프 내에 있습니다. 컬렉션 요소의 값을 수정하는 것은 컴파일 시간 오류입니다. 따라서 구조체를 포함하여 값 형식인 요소를 수정할 수 없습니다. 요소가 참조 형식인 컬렉션에서 각 요소의 액세스 가능한 멤버를 수정할 수 있지만 전체 요소를 추가하거나 제거하거나 바꾸려면 컴파일러 오류 CS1656이 생성됩니다.
Example
다음 예제는 Book가 struct이므로 오류 CS1654를 생성합니다. 오류를 해결하려면 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
}
}
}
}
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET