Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is frequently confusing, and I think the MSDN documentation actually is a bit "clear only if already known". That is, it is correct, but it really only makes sense if you already understand the concept.</p> <p>Here's how I think of it.</p> <p>A regular parameter makes a <em>copy</em> of the <em>value</em> of the <em>argument</em>. When you say:</p> <pre><code>static int M(int z) { z = z + 1; return z; } ... int x = 123; int y = M(x); </code></pre> <p>That is just like you said:</p> <pre><code>int x = 123; int z = x; // make a copy of x z = z + 1; int y = z; </code></pre> <p>A ref or out parameter <em>make an alias for an existing variable</em>. When you say</p> <pre><code>static void N(ref int q) { q = q + 1; } ... int x = 123; N(x); </code></pre> <p>That is the same as saying:</p> <pre><code>int x = 123; // MAGIC: q is now an another name for variable x q = q + 1; </code></pre> <p><code>q</code> and <code>x</code> are <em>two different names</em> that refer to <em>the same variable</em>. Incrementing <code>q</code> also increments <code>x</code> because they are the same. <code>z</code> and <code>x</code> in the previous example are <em>two different names</em> that refer to <em>two different variables</em>. Incrementing <code>z</code> does not change <code>x</code>.</p> <p>Summing up: "out" and "ref" just mean "do not make a new variable; rather, temporarily make a second name for an existing variable". </p> <p>Is that now clear?</p> <p>UPDATE: I did not say what the difference between "out" and "ref" is. The difference is simple. On the "caller" side, a "ref" must be a definitely assigned variable <em>before</em> the method is called. An "out" need not be. On the "callee" side, a "ref" may be read before it is written to, but an "out" must be written to before it is read. Also, an "out" must be written to before control leaves the method normally.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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