과제에 대한 질문이 있습니다.목록의 인스턴스 참조 변경
public class A {}
public class AHolder
{
public A AnInstance;
}
void Change()
{
A anotherInstance=new A();
anotherInstance.aField="bla";
A anotherInstance2=new A();
anotherInstance2.aField="blabla";
List<AHolder> aList= new List<AHolder>();
aList.add(new AHolder(){AnInstance=anotherInstance});
aList.add(new AHolder(){AnInstance=anotherInstance});
aList.add(new AHolder(){AnInstance=anotherInstance});
anotherInstance=anotherInstance2;
}
어떻게 anotherInstance 루프를 사용하지 않고 변경하는 경우, aList의 모든 AnInstance 값의 변화를 보장하는 코드를 구현 할 수 있습니까?
업데이트 : 위 코드 줄을 실행 한 후 aList [0] .AnInstance.aField.Is에서 "blabla"값을 가져 오려고합니다.
당신은 할 수 없습니다. 그 이유는 실제로 * 로컬 변수의 값을 변경하는 것입니다 *; 당신의'AHolder' 인스턴스는 지역 변수에 대해 아무것도 모른다. 그들은 당신이 그들에게 하나의 인스턴스를 전달했음을 알고 있지만 당신이 그것을 어디에서 얻었는지 상관하지 않습니다. 해결하려는 특정 시도가 아니라 해결하려는 문제를 설명하십시오. – Jon