我们认为这将是一种通过值限制绕过结构传递的简单方法(在现实生活中,我们的结构有更多成员)但我们注意到在结构中使用委托不会修改结构内部成员,即使那是委托数组的唯一用途。
鉴于此委托和结构:
public delegate void ChangeValues();
structure Effects
{
int val1,val2,val3;
void SetValues(int index)
{
ChangeValues[] delegateArray = new ChangeValues(){this.SetValSet1,this.setValSet2,this.SetValSet3};
delegateArray[index](); //now set the values as necessary
}
void setValSet1()
{
val1=0;val2=1;val3=2;
}
void setValSet2()
{
val1=1;val2=2;val3=3;
}
void setValSet3()
{
val1=2;val2=3;val3=4;
}
}
Effects effects = new Effects();
effects.SetValues(1); //when stepping through code one sees the values change inside eh setValues functions, but the modified values are gone after leaving this function
使用委托会导致结构的另一个实例被创建然后丢失吗?