나는 사용자 정의 클래스의 상수 정적 컬렉션을 만들과 같이하기 위해 노력하고있어 : 만들 MyClass에정적 상수 사용자 정의 클래스
static class MyObjects
{
public const MyClass anInstanceOfMyClass = { Property1 = "foo", Property2 = "bar" };
}
의 상수 정적 객체의 클래스를 다음
public class MyClass
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
그러나 컴파일러는 "Property1"및 "Property2"이름이 현재 컨텍스트에 존재하지 않는다고 불평합니다. 또한 내가 이것을 할 때 :
public const MyClass anInstanceOfMyClass = new MyClass() { Property1 = "foo", Property2 = "bar" };
컴파일러는 Property1과 Property2가 읽기 전용 인 것에 대해 불평합니다. 이 MyClass 객체의 상수 정적 클래스를 올바르게 초기화하려면 어떻게해야합니까?
는 @ThoughtCrhyme @Daniel는 말. 값 유형과'string'을'const'와 함께 사용할 수 있기 때문에 대신'static'을 사용하십시오. –