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 6664427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:36:34+00:00 2026-05-26T02:36:34+00:00

(FYI: This question is half thoretical. It is not something which I am definatly

  • 0

(FYI: This question is half thoretical. It is not something which I am definatly planning on doing.)

I would like to be able to keep a reference to all the objects that I create. Maybe like this:

class Foo
{
    private static List<Foo> AllMyFoos = new List<Foo>();

    public Foo()
    {
        AllMyFoos.Add(this);
    }
}

The trouble with this is that now none of my Foos can ever drop out of focus and be garbage collected. Is there any way of keeping a referance without getting in the way of the garbage collector?

Ideally I just of a list of Foos that are still being used, not all Foos which have ever been used.

  • 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-05-26T02:36:34+00:00Added an answer on May 26, 2026 at 2:36 am

    Use WeakReference – it does exactly what it is supposed to. Be careful while working with it – you have to check if the reference is still valid every time you dereference it.

    Tutorial.


    Foo foo = AllMyFoos[index].Target as Foo;
    if (foo == null)
    {
       // Object was reclaimed, so we can't use it.
    }
    else
    {
       // foo is valid. My theoretical curiosity can be satisfied
    }
    

    Warning: Just because the object hasn’t yet been garbage collected doesn’t mean someone hasn’t called Dispose on it, or in some other way put it into a state that it is not prepared to be used again.

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

Sidebar

Related Questions

FYI, There is some overlap in the initial description of this question with a
I've already found this question on SO (which is exactly the same problem I'm
So, this may be a really stupid question, but I'm obviously missing something here.
This question is based on a previous , but that's just FYI. I've managed
This sounds like a stupid question but, after uploading a file the file isnt
FYI, This is very similar to my last question: Is there a faster way
I've read this post: ( NSDateFormatter leak question ) which states that NSDateFormatter leaks
FYI I'm programming in objective-C but anyone might be able to help. I've been
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
I have looked everywhere for the answer to this question and while there are

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.