Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Thursday, April 15, 2004 9:45 AM by mswanson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Setting Null to local variables

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!
Sunday, May 02, 2004 1:20 PM by Derek Beyer

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker