在C#中通過引用或值傳遞對象在C#中,我一直認(rèn)為非原始變量是通過引用傳遞的,而原語值是通過值傳遞的。因此,當(dāng)將任何非原始對象傳遞給方法時(shí),對方法中的對象所做的任何操作都會(huì)影響所傳遞的對象。(C#101)然而,我注意到當(dāng)我通過System.Drawing.ImageObject時(shí),情況似乎并非如此?如果我將一個(gè)system.draing.Image對象傳遞給另一個(gè)方法,并將一個(gè)圖像加載到該對象上,那么讓該方法超出作用域并返回到調(diào)用方法時(shí),該圖像不會(huì)加載到原始對象上?這是為什么?
3 回答

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
對象
out
ref
public void Foo(Image image){ // This change won't be seen by the caller: it's changing the value // of the parameter. image = Image.FromStream(...);}public void Foo(ref Image image){ // This change *will* be seen by the caller: it's changing the value // of the parameter, but we're using pass by reference image = Image.FromStream(...);}public void Foo(Image image){ // This change *will* be seen by the caller: it's changing the data // within the object that the parameter value refers to. image.RotateFlip(...);}
- 3 回答
- 0 關(guān)注
- 801 瀏覽
添加回答
舉報(bào)
0/150
提交
取消