Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the 'ref' keyword when the you need to modify <em>what the reference is pointing to</em>. When you pass a reference type into a method it <em>is</em> passed by value, but the value is a <em>copy</em> of that reference which is passed to the method. This means that you can change the general state (i.e., properties/fields) of the referred to object, but if you attempt to change what the reference points to you will only affect the copy.</p> <p>For example, given this method...</p> <pre><code>private void Foo( MyClass obj ) { obj = new MyClass( ); obj.SomeProperty = true; } </code></pre> <p>We can pass in the argument and then see if it was affected:</p> <pre><code>MyClass test = new MyClass( ); test.SomeProperty = false; Foo( test ); Console.WriteLine( test.SomeProperty ); // prints "False" </code></pre> <p>Now, if we had defined the method using the 'ref' keyword...</p> <pre><code>private void Foo( ref MyClass obj ) { obj = new MyClass( ); obj.SomeProperty = true; } </code></pre> <p>The output would be "True", because the actual reference was passed to the method, not a copy. We changed what that reference points to within the function and we see the effects of those changes.</p> <p>You are just creating a new pointer to the object on the heap when you omit the 'ref' keyword. If you change one pointer you will not change the other.</p> <p>...</p> <p>So, to answer your question; no, you do not need to use the 'ref' keyword to change the state of your single Cache object when it is passed to a method.</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. 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