컴파일러 오류 CS1917
업데이트: 2007년 11월
오류 메시지
형식이 'struct name'인 읽기 전용 필드 'name'의 멤버는 값 형식이므로 개체 이니셜라이저를 사용하여 할당할 수 없습니다.
Members of read-only field 'name' of type 'struct name' cannot be assigned with an object initializer because it is of a value type.
값 형식의 읽기 전용 필드는 생성자에서만 할당할 수 있습니다.
이 오류를 해결하려면
구조체를 클래스 형식으로 변경합니다.
생성자를 사용하여 구조체를 초기화합니다.
예제
다음 코드에서는 CS1917 오류가 발생하는 경우를 보여 줍니다.
// cs1917.cs
class CS1917
{
public struct TestStruct
{
public int i;
}
public class C
{
public readonly TestStruct str = new TestStruct();
public static int Main()
{
C c = new C { str = { i = 1 } }; // CS1917
return 0;
}
}
}