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

前端 未结 1 1476
北海茫月
北海茫月 2020-12-13 03:56

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 w

相关标签:
1条回答
  • 2020-12-13 04:38

    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.

    0 讨论(0)
提交回复
热议问题