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

  • Home
  • SEARCH
  • 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 8985555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:21:25+00:00 2026-06-15T21:21:25+00:00

Lets say I have something like: public class Item { public string Code; public

  • 0

Lets say I have something like:

public class Item
{
    public string Code;
    public List<string> Codes = new List<string>();
}

private void SomeMethod()
{
    List<Item> Items = new List<Item>();
    for (int i = 0; i < 10; i++)
    {
        Item NewItem = new Item();
        NewItem.Code = "Something " + i.ToString();
        NewItem.Codes.Add("Something " + i.ToString());
        Items.Add(Item);
    }

    //Do something with Items
}

I am instantiating Item and not free’ing it because I need to have access to it in the list later on (rough example).

What I am wondering is when SomeMethod() has finished executing, will Items (and the contents of it – including the List<>) be de-referenced and allow the garbage collector to clean up the memory as and when it runs? Basically, will this section of code cause any memory leaks or should everything be de-referenced when SomeMethod() has finished processing.

My understanding is that when nothing holds a reference to an object it will be garbage collected so in my mind this code should be Ok but I just wanted to make sure I understand correctly.

EDIT:

If I was to add one of the objects Items is holding into another list that would still be in scope (a global list for example). What would happen?

  • 1 1 Answer
  • 0 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-06-15T21:21:25+00:00Added an answer on June 15, 2026 at 9:21 pm

    Once your variable Items goes out of scope, the garbage collector will indeed dispose of it (and its contents, as your code is written) at its leisure.

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

Sidebar

Related Questions

lets say I have a class like this: public class MyClass { private String
lets say I have something like this: class Program { static void Main(string[] args)
Lets say we have the following code class Image { public: Image(const std::string &
Lets say I have a class that is doing something like: public class Foo
Lets say i have the following scenario: public class A { public String createString(final
Let's say we have a Collection of Items: class Item { public String title;
Lets say I have a set of model classes like this: public class Person
Lets say I have a custom data type that looks something like this: public
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
Possible Duplicate: C printing bits Lets say I have a uint16_t number something like:

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.