通过C#中的引用传递对象和对象列表 [英] Passing objects and a list of objects by reference in C#

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

问题描述

我有一个修改对象的委托。我从调用方法中将一个对象传递给委托,但是调用方法不会获取这些更改。如果我通过 List 作为对象,则相同的代码有效。

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 pick up 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. Is that correct?

我可以修改代码以将 ref 对象传递给委托。但是我想知道为什么这是必要的。

I can modify my code to pass a ref object to the delegate. But I am wondering why this is necessary. Or is it?

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
}

更新:

我现在通过 ref 在我实例化 BindMyObject 中的新对象时。

I am now passing objects by ref to the delegate as I am 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.

默认情况下,参数值是通过value传递的-无论该值是值类型值还是引用。如果通过该引用修改了对象,则调用代码也将看到该更改。

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.

在您最初显示的代码中,没有理由使用 ref ref 关键字用于需要更改 parameter 的值的方法(例如,使其完全引用其他对象)并且

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();
}

或类似这样的代码:

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

?如果是后者,则不需要 ref 。如果是前者,则 do 需要 ref ,因为您要更改参数本身,而不是更改值所引用的对象。实际上,如果只是设置 obj 的值而从未读过它,则应该使用 out 而不是 ref

? 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天全站免登陆