Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 529
David Avatar asked Jun 14 '10 19:06

David


People also ask

Are objects passed by reference in C?

There are references in C, it's just not an official language term like it is in C++. "reference" is a widely used programming term that long predates the C++ standardizing of it. Passing a pointer to an object is passing that object by reference.

Can you pass an object by reference?

A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value. “Passing by value” refers to passing a copy of the value. “Passing by reference” refers to passing the real reference of the variable in memory.

What is passed when an object is passed by reference?

What exactly is passed when an object is passed by reference? Explanation: The location of the object, that is, the exact memory location is passed, when the object is passed by reference.


1 Answers

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.

like image 195
Jon Skeet Avatar answered Sep 17 '22 15:09

Jon Skeet