Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:04:57+00:00 2026-05-16T14:04:57+00:00

Suppose there is the following code: foreach (…) { List<int> localList = new List<int>(100);

  • 0

Suppose there is the following code:

foreach (...)
{
    List<int> localList = new List<int>(100);

    // do stuff

    localList = null;
}

From time to time, I am tempted to null references just as the procedure is about to exit (that is, return) or in this case, just as it is about to loop. Is there any benefit to doing so, even if a small one?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:04:58+00:00Added an answer on May 16, 2026 at 2:04 pm

    There is no benefit to doing this for local variables. The CLR knows, by means of the JIT, exactly when a local variable is no longer used within a method and hence collectable.

    Raymond Chen recently did a very in depth blog article on exactly when objects are collectible . It covers this scenario in detail and is worth the read

    • http://blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048149.aspx

    There is however a couple of exceptions to this rule. If the local variable in question is captured into a closure or an iterator then yes nulling out the variable has an effect. Namely because the local is no longer a local but instead is a field and has different GC semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following code: foreach(Item i on ItemCollection) { Something s = new Something();
Suppose I have the following code: List<SomeObject> someObjects = ReturnListWithThousandsOfObjects(); foreach(SomeObject someobject in someObjects)
There is unsurprisingly a run time exception thrown by the following code : data
suppose that there is given following matlab code function dp = derp(p) n =
Suppose there is a list that is input from the console using <STDIN> @string
Suppose we have the following code: void AFunction() { foreach(AClass i in AClassCollection) {
Propose to consider the following problem. Suppose we have some composite object. Are there
Suppose there are two scripts Requester.php and Provider.php, and Requester requires processing from Provider
Suppose I've following Code: Console.WriteLine(Value1: + SomeEnum.Value1.ToString() + \r\nValue2: + SomeOtherEnum.Value2.ToString()); Will Compiler Optimize
Suppose that I have the following code: private void UpdateDB(QuoteDataSet dataSet, Strint tableName) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.