Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have:</p> <pre><code>string Method1() { return new Random().Next(0, 1000).ToString(); } </code></pre> <p>Then when you call it, it creates a <code>Random</code> object, and soon afterwards a <code>string</code> object.</p> <p>Once the method is done using the <code>Random</code> object by calling <code>Next</code> (note, whether the method has returned or not has absolutely <strong>nothing</strong> to do with this, regardless of what that book might say). there's no more references to it anywhere that any code will reach it by, so it might be collected.</p> <p>If though the string was obtained in:</p> <pre><code>void Method2() { string s = Method1(); //s isn't collected here by devious forces. Console.WriteLine(s);//this works. } </code></pre> <p>So the book is saying "things don't magically disappear when you use them.</p> <p>It's actually incorrect, in:</p> <pre><code>void Method3() { Method1(); } </code></pre> <p>There's no reason to suspect that the string wasn't collected before the method method returned.</p> <p>But also:</p> <pre><code>static string blah void Method4() { blah = new Random().Next(0, 10).ToString(); } </code></pre> <p>This didn't return anything, but it created an object that won't be collected.</p> <p>Also:</p> <pre><code>void Method5(out string ret) { ret = new Random().Next(0, 10).ToString(); } void Method 6() { string s; Method5(out s); //s isn't collected here by devious forces. Console.WriteLine(s);//this works. } </code></pre> <p>Here's a better version:</p> <p>"Once there is no way that any code which will run could use the object, it may be collected."</p> <p>Buy a better book, there's no point asking people to explain what Herbert Schildt says about something, because that assumes he's correct. Frankly, I'd be more worried about the bits you thought you understood than the bits you were confused by, because you won't know if it was actually correct or bullschildt.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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