Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

Passing objects and a list of objects by reference in C#

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?

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
}

Update:

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);
question from:https://stackoverflow.com/questions/3040170/passing-objects-and-a-list-of-objects-by-reference-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...