0
我們認爲這將是一個簡單的方法來解決通過價值限制(現實生活中我們的結構有更多成員)的結構傳遞,但我們注意到使用結構內的代表不會修改結構內部成員,即使這是代表數組的唯一目的。
鑑於此委託和結構:
在結構中使用代表不會修改結構內部元素
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
是否使用委託會導致創建結構的另一個實例,然後失去了什麼?