C# Frequently Asked Questions

The C# team and other members of the C# community are answering some Frequently Asked Questions via a blog that rolls up to a page on MSDN. I’ve been asked a number of these questions myself by partners and customers, so this should serve as an excellent resource going forward.

Comments (1)

  1. Derek Beyer says:

    I think this one is particularly interesting. its from the <a href="http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx">C# FAQ Blog. </a>

    I knew that setting variables to null was pointless since the GC took care of that for you, but i didn’t realize that you could actually extend the life of you types if you set them to null.

    Consider the following:

    void SomeMethod()


    string s;

    // initialize and use string s here

    // go do some other long running stuff

    // finally set string s = null



    If i understand this correctly, the FAQ post is saying that if the GC runs after you’ve used s and before you set it to nothing, it will see that s is still being used (by virtue of the fact that you are setting it to null before you return) and will not consider it garbage. Interesting.

    Please consider this a formal customer feature request 😉 : in v? i’d like the JIT to see where i am setting things to null and replace it with a call to Dispose, and if there isn’t a Dispose available, it should remove the assignment. thanks!