在C#中通过引用或值传递对象在C#中,我一直认为非原始变量是通过引用传递的,而原语值是通过值传递的。因此,当将任何非原始对象传递给方法时,对方法中的对象所做的任何操作都会影响所传递的对象。(C#101)然而,我注意到当我通过System.Drawing.ImageObject时,情况似乎并非如此?如果我将一个system.draing.Image对象传递给另一个方法,并将一个图像加载到该对象上,那么让该方法超出作用域并返回到调用方法时,该图像不会加载到原始对象上?这是为什么?
3 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
对象
outref
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 关注
- 826 浏览
添加回答
举报
0/150
提交
取消
