C#按引用传递对象的对象和列表 [英] C# Passing objects and list of objects by reference

查看:117
本文介绍了C#按引用传递对象的对象和列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有修改对象的委托。我传递一个对象给委托从调用的方法,但是调用方法不皮卡这些变化。如果我通过一个List作为对象相同的代码工作。我以为所有的对象都按引用传递的,所以任何修改,将在调用方法得到体现?



我可以修改我的代码通过一个参考对象委托,但我想知道为什么这是必要的。

 公共类粘合剂
{
保护委托INT MyBinder< T>(对象读卡器,T myObject的);

公共无效BindIt< T>(对象的读者,T myObject的)
{
// m_binders是粘结剂的哈希表对象
MyBinder< T>粘合剂= m_binders [测试]作为MyBinder< T> ;;
INT I =粘结剂(读卡器,myObject的);
}
}

公共类MyObjectBinder
{
公共MyObjectBinder()
{
m_delegates [测试] =新MyBinder<&MyObject的GT;(BindMyObject);
}

私人诠释BindMyObject(读者对象,myObject的OBJ)
{
OBJ =新MyObject的
{
//更新属性
};
返回1;
}
}

///在其它的类
公共无效CallingMethod()
{
MyObject的OBJ =新MyObject来调用方法();

MyObjectBinder粘结剂=新MyObjectBinder();
binder.BindIt(myReader,OBJ); //不用担心myReader

// OBJ应该表现出反映变化
}

更新时间:



我传递的对象由裁判委托,因为我是实例里面BindMyObject一个新的对象。

 保护委托INT MyBinder< T>(读者对象,楼盘牛逼myObject的); 


解决方案

对象不按引用传递。 。对象是不是在所有通过



默认情况下,该参数的值是按值传递 - 这值是值类型值或引用。如果一个对象通过该引用修改,那么这种改变将是调用代码以及可见的。



在你的代码显示最初,没有理由使用 REF 。当你想改变的值的方法的 REF 关键字使用的参数的(例如,使其指向一个不同的对象完全)和。有变化给调用者可见



现在,在你的代码已经证明(原来是)你只得到了:

 私人诠释BindMyObject(读者对象,myObject的OBJ)
{
//更改在这里OBJ
}

你的意思是这样的代码:

 私人诠释BindMyObject(读者对象,myObject的OBJ)
{
OBJ =新MyObject来();
}

或像这样的代码:

 私人诠释BindMyObject(读者对象,myObject的OBJ)
{
obj.SomeProperty = differentValue;
}



?如果是后者,那么你不需要 REF 。如果是前者,那么你的的需要 REF ,因为你改变参数本身,而不是更改该对象的值是指。事实上,如果你的只是的设置 OBJ 的值,而没有阅读它,你应该使用退出而不是 REF



如果你能证明一个简短而完整程序,它展示了你的问题,这将是一个更容易解释这是怎么回事



这是很难做到这一点的话题正义在短短的几段 - 所以我有一个一下整篇文章,这将有望使事情变得更加明显。


I have a delegate that modifies an object. I pass an object to the delegate from a calling method, however the calling method does not pickup these changes. The same code works if I pass a List as the object. I thought all objects were passed by reference so any modifications would be reflected in the calling method?

I can modify my code to pass a ref object to the delegate but am wondering why this is necessary?

public class Binder
{
    protected delegate int MyBinder<T>(object reader, T myObject);

    public void BindIt<T>(object reader, T myObject)
    {
        //m_binders is a hashtable of binder objects
        MyBinder<T> binder = m_binders["test"] as MyBinder<T>;
        int i = binder(reader, myObject);
    }
}

public class MyObjectBinder
{
    public MyObjectBinder()
    {
        m_delegates["test"] = new MyBinder<MyObject>(BindMyObject);
    }

    private int BindMyObject(object reader, MyObject obj)
    {
        obj = new MyObject
        {
            //update properties
        };
        return 1;
    }
}

///calling method in some other class
public void CallingMethod()
{
    MyObject obj = new MyObject();

    MyObjectBinder binder = new MyObjectBinder();
    binder.BindIt(myReader, obj); //don't worry about myReader

    //obj should show reflected changes
}

UPDATED

I passed objects by ref to the delegate as I was instantiating a new object inside BindMyObject.

protected delegate int MyBinder<T>(object reader, ref T myObject);

解决方案

Objects aren't passed by reference. Objects aren't passed at all.

By default, the value of the argument is passed by value - whether that value is a value type value or a reference. If an object is modified via that reference, then that change will be visible to the calling code as well.

In the code you showed originally, there was no reason to use ref. The ref keyword is used when you want a method that changes the value of a parameter (e.g. to make it refer to a different object entirely) and have that change visible to the caller.

Now, in the code you've shown (originally) you've only got:

private int BindMyObject(object reader, MyObject obj)
{
    //make changes to obj in here
}

Do you mean code like this:

private int BindMyObject(object reader, MyObject obj)
{
    obj = new MyObject();
}

or code like this:

private int BindMyObject(object reader, MyObject obj)
{
    obj.SomeProperty = differentValue;
}

? If it's the latter, then you don't need ref. If it's the former, then you do need ref because you're changing the parameter itself, not making changes to the object that the value refers to. In fact, if you're just setting the value of obj without ever reading it, you should use out instead of ref.

If you can show a short but complete program which demonstrates your problem, it'll be a lot easier to explain what's going on.

It's hard to do this topic justice in just a few paragraphs - so I've got an entire article about it, which will hopefully make things more obvious.

这篇关于C#按引用传递对象的对象和列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆