C# Frequently Asked Questions

C# Frequently Asked Questions

  • Comments 1

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.

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
  • 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
    return;
    }

    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!
Page 1 of 1 (1 items)