How GC collects resources in a static member in C#?

Posted by carter-boater on Stack Overflow See other posts from Stack Overflow or by carter-boater
Published on 2010-03-31T19:24:04Z Indexed on 2010/03/31 19:33 UTC
Read the original article Hit count: 437

Filed under:
|

Dear all,

I have a piece of code like this:

Class Program
{
static StreamReader sr = null;
static int var=0;
static Program()
{
sr = new StreamReader("input.txt")
}
~Program()
{
sr.Dispose();
}

static void main(string args[])
{
//do something with input here
}

}

This may not be a good practice, but I just want to use this example to ask how the deconstructor and GC works. My question is: Will ~Program() get called at a non-determined time or it won't be called at all in this case. If the deconstructor won't get called, then how GC collect the unmanaged resources and managed resources.

Thank you very much!

© Stack Overflow or respective owner

Related posts about c#

Related posts about garbage-collection