title: When do references of objects go out of scope?
body: <p>I have been asking myself for quite a while, when exactly objects and variables go out of scope and are garbage collected. Also what the difference between reference and value types is in this regard.</p>
  
  <p>For example if I use</p>
  
  <pre><code>Private Sub Foo()
    Dim ob as Integer = 1
  End Sub
  </code></pre>
  
  <p>it is my understanding that the variable goes out of scope when the Sub has run its course and the related memory is freed again. I thought that this was the same with references. However I can do the following</p>
  
  <pre><code>Private Sub Foo()
    Dim frm1 as New Form1
    frm1.Show()               'Edit made here!
  End Sub
  </code></pre>
  
  <p>and the related form is opened stays open even though the sub is over immediately. Therefore there seem to be references that are kept open somehow, so the new form instance is not disposed of. Maybe there is something special about forms.
  But what about some other random object?</p>
  
  <pre><code>Private Sub Foo()
    Dim ob as New MyRandomClass
  End Sub
  </code></pre>
  
  <p>Will this object persist like the form or is it disposed? </p>
  
  <p>Could someone shed some light about the details of scope and the differences between variables, objects and maybe Forms (which are not really straightforward in VB.net compared to C# in my opinion).</p>