Scope of Refereances to Instances
-
Hi, I am just beginner in .NET with C#. In my study, I have learned that when an instance of a class created in the heap is not owned by reference (a variable of that class) , the Garbage Collector (GC) removes this instance from the memory after a while. In my first small application in .NET, I hasitate in a point that When I am testing a click event of a button control of my Form1 class, I declared a variable of type a Form class (let's say Form2). The View is like this: private void button1_click(....) { Form2 myForm2 = new Form2(); myForm2.Show(); } Here when the execution gets out of the click event, then I expect that the local "myForm2" becomes out of scope for availibility. But my new form (Form2) still apears on my screen. Why? Cause no availiable reference to this Form2 instance anymore, immediately after the button click event routine. Anything I need to know more about "scopes" and GC ?
-
Hi, I am just beginner in .NET with C#. In my study, I have learned that when an instance of a class created in the heap is not owned by reference (a variable of that class) , the Garbage Collector (GC) removes this instance from the memory after a while. In my first small application in .NET, I hasitate in a point that When I am testing a click event of a button control of my Form1 class, I declared a variable of type a Form class (let's say Form2). The View is like this: private void button1_click(....) { Form2 myForm2 = new Form2(); myForm2.Show(); } Here when the execution gets out of the click event, then I expect that the local "myForm2" becomes out of scope for availibility. But my new form (Form2) still apears on my screen. Why? Cause no availiable reference to this Form2 instance anymore, immediately after the button click event routine. Anything I need to know more about "scopes" and GC ?
As u have created reference of form2 from form1's button click event so this reference of form2 will remain in scope till the form1 is in scope and as soon u will close the form1 the reference of form2 will also be lost and GC will reclaim the memory back and will close the form2 also.
rahul
-
As u have created reference of form2 from form1's button click event so this reference of form2 will remain in scope till the form1 is in scope and as soon u will close the form1 the reference of form2 will also be lost and GC will reclaim the memory back and will close the form2 also.
rahul
First, I'd really like to thanks to you rah_sin for your answer.. I need one more point: What you explain is valid for only class of Form or those inherited from Form class? Cause as you hv seen in my example the refernce "myForm2" is not a direct member of Form1 class but a local declaration in button click event. Or should I understand that Show() method of Form class, saves or register its reference into a global collection internally or somewhere in caller form ? In other words, what if my locally declared variable was of simple class like: class Sample { private string sName; private int nIdNumber; public Sample() { //... } public string Name { get {...} set {...} } //... } private button_click(....) { Sample mySample = new Sample(); mySample.Name = "Testing Name"; }