Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Objects aren't passed by reference. Objects aren't passed at all.</p> <p>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.</p> <p>In the code you showed originally, there was no reason to use <code>ref</code>. The <code>ref</code> keyword is used when you want a method that changes the value of a <em>parameter</em> (e.g. to make it refer to a different object entirely) and have that change visible to the caller.</p> <p>Now, in the code you've shown (originally) you've only got:</p> <pre><code>private int BindMyObject(object reader, MyObject obj) { //make changes to obj in here } </code></pre> <p>Do you mean code like this:</p> <pre><code>private int BindMyObject(object reader, MyObject obj) { obj = new MyObject(); } </code></pre> <p>or code like this:</p> <pre><code>private int BindMyObject(object reader, MyObject obj) { obj.SomeProperty = differentValue; } </code></pre> <p>? If it's the latter, then you don't need <code>ref</code>. If it's the former, then you <em>do</em> need <code>ref</code> because you're changing the parameter itself, not making changes to the object that the value refers to. In fact, if you're <em>just</em> setting the value of <code>obj</code> without ever reading it, you should use <code>out</code> instead of <code>ref</code>.</p> <p>If you can show a short but <em>complete</em> program which demonstrates your problem, it'll be a lot easier to explain what's going on.</p> <p>It's hard to do this topic justice in just a few paragraphs - so I've got an <a href="http://pobox.com/~skeet/csharp/parameters.html" rel="noreferrer">entire article about it</a>, which will hopefully make things more obvious. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload