Click here to Skip to main content
15,921,959 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to convert string to form object in c# window application Pin
ExcellentOrg6-Aug-13 9:22
ExcellentOrg6-Aug-13 9:22 
AnswerRe: How to convert string to form object in c# window application Pin
Jean A Brandelero8-Aug-13 10:45
Jean A Brandelero8-Aug-13 10:45 
GeneralRe: How to convert string to form object in c# window application Pin
susanna.floora9-Aug-13 2:02
susanna.floora9-Aug-13 2:02 
GeneralRe: How to convert string to form object in c# window application Pin
Jean A Brandelero9-Aug-13 7:51
Jean A Brandelero9-Aug-13 7:51 
AnswerRe: How to convert string to form object in c# window application Pin
BillWoodruff11-Aug-13 0:13
professionalBillWoodruff11-Aug-13 0:13 
GeneralRe: How to convert string to form object in c# window application Pin
susanna.floora26-Aug-13 21:47
susanna.floora26-Aug-13 21:47 
QuestionAutocomplete TextBox no find other words in string Pin
juliogyn5-Aug-13 16:49
juliogyn5-Aug-13 16:49 
AnswerRe: Autocomplete TextBox no find other words in string Pin
Abhinav S5-Aug-13 17:07
Abhinav S5-Aug-13 17:07 
GeneralRe: Autocomplete TextBox no find other words in string Pin
juliogyn5-Aug-13 18:08
juliogyn5-Aug-13 18:08 
AnswerRe: Autocomplete TextBox no find other words in string Pin
Abhinav S5-Aug-13 18:31
Abhinav S5-Aug-13 18:31 
GeneralRe: Autocomplete TextBox no find other words in string Pin
juliogyn6-Aug-13 9:18
juliogyn6-Aug-13 9:18 
QuestionTry, Catch, Finally question Pin
JD865-Aug-13 15:15
JD865-Aug-13 15:15 
AnswerRe: Try, Catch, Finally question Pin
Abhinav S5-Aug-13 16:45
Abhinav S5-Aug-13 16:45 
GeneralRe: Try, Catch, Finally question Pin
JD866-Aug-13 6:18
JD866-Aug-13 6:18 
AnswerRe: Try, Catch, Finally question Pin
ExcellentOrg5-Aug-13 22:06
ExcellentOrg5-Aug-13 22:06 
AnswerRe: Try, Catch, Finally question Pin
Bernhard Hiller5-Aug-13 22:46
Bernhard Hiller5-Aug-13 22:46 
GeneralRe: Try, Catch, Finally question Pin
JD866-Aug-13 6:18
JD866-Aug-13 6:18 
GeneralRe: Try, Catch, Finally question Pin
Jean A Brandelero8-Aug-13 10:47
Jean A Brandelero8-Aug-13 10:47 
QuestionClick-Once :: Publishing And Updates In Different Locations Pin
Matt U.5-Aug-13 8:17
Matt U.5-Aug-13 8:17 
AnswerRe: Click-Once :: Publishing And Updates In Different Locations Pin
Pete O'Hanlon6-Aug-13 23:05
mvePete O'Hanlon6-Aug-13 23:05 
GeneralRe: Click-Once :: Publishing And Updates In Different Locations Pin
Matt U.7-Aug-13 2:01
Matt U.7-Aug-13 2:01 
QuestionC# Garbage Collection and GC.Collect() Pin
DSLoginYea5-Aug-13 7:38
DSLoginYea5-Aug-13 7:38 
I was testing the garbage collection in C#, and came across something that doesn't make sense to me. I know about Garbage Collection happening "randomly", but I am using GC.Collect() here. Please see code below and then the result information:

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Method();
            Method2();

        }

        void Method()
        {

            TestA b = new TestA("first one");


            for (int i = 0; i < 10; i++)
            {
                TestA a = new TestA();
            }
            GC.Collect();
        }

        void Method2()
        {

        }

    }

    public class TestA
    {
        string MSG;

        public TestA()
        {
            MSG = "Not first one";
        }

        public TestA(string msg)
        {
            MSG = msg;
        }


        ~TestA()
        {

            MessageBox.Show("Destructing A " + MSG);

        }



    }



I am getting "Not first one" (destructor and I am assuming GC'ed) 9 times. Then when I close the form I am getting "first one" 1 time and then "Not first one" 1 more time.

My main question is why isn't instance b of TestA being collected on GC.Collect? Are the ones in the for loop falling out of scope, but not the one before my for loop? If so, please explain what is keeping it in scope as Method() has completed.

Thank you.

Respectfully,

D
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Eddy Vluggen5-Aug-13 7:51
professionalEddy Vluggen5-Aug-13 7:51 
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Abhinav S5-Aug-13 17:00
Abhinav S5-Aug-13 17:00 
AnswerRe: C# Garbage Collection and GC.Collect() Pin
Bernhard Hiller5-Aug-13 22:49
Bernhard Hiller5-Aug-13 22:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.