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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:44:30+00:00 2026-05-12T05:44:30+00:00

How can I count the number of objects of a class type within a

  • 0

How can I count the number of objects of a class type within a method of that class? For that matter, how to do it outside of a class without adding the objects to a list?

I should have thought of that! Thanks! I’m gonna leave it unanswered for a little while to see if there is a better way, because I agree. I’m just sortv wrapping my head around OO. If you don’t mind let me explain a little more and maybe there is a better way in general?

I have an object class that i want to add 3 pieces of information to, but first I want to cycle through and make sure there are no other objects with any of the three pieces the same, and if there are, do something different for each case.

  • 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-12T05:44:30+00:00Added an answer on May 12, 2026 at 5:44 am

    The only way to accomplish what you’re looking for is to keep a static list of these objects in the class itself. If you just want to see if there is an instance somewhere that hasn’t been garbage collected, then you’ll want to use the WeakReference class. For example…

    public class MyClass
    {
        private static List<WeakReference> instances = new List<WeakReference>();
    
        public MyClass()
        {
             instances.Add(new WeakReference(this));
        }
    
        public static IList<MyClass> GetInstances()
        {
            List<MyClass> realInstances = new List<MyClass>();
            List<WeakReference> toDelete = new List<WeakReference>();
    
            foreach(WeakReference reference in instances)
            {
                if(reference.IsAlive)
                {
                    realInstances.Add((MyClass)reference.Target);
                }
                else
                {
                    toDelete.Add(reference);
                }
            }
    
            foreach(WeakReference reference in toDelete) instances.Remove(reference);
    
            return realInstances;
        }
    }
    

    Since you’re new to OO/.NET, don’t let the WeakReference use scare you. The way garbage collection works is by reference counting. As long as some piece of code or an object has access to a particular instance (meaning it’s within scope as a or as part of a local, instance, or static variable) then that object is considered alive. Once that variable falls OUT of scope, at some point after that the garbage collector can/will collect it. However, if you were to maintain a list of all references, they would never fall out of scope since they would exist as references in that list. The WeakReference is a special class allows you to maintain a reference to an object that the garbage collector will ignore. The IsAlive property indicates whether or not the WeakReference is pointing to a valid object that still exists.

    So what we do here is keep this list of WeakReferences that point to every instance of MyClass that’s been created. When you want to obtain a list of them, we iterate through our WeakReferences and snatch out all of them that are alive. Any we find that are no longer alive are placed into another temporary list so that we can delete them from our outer list (so that the WeakReference class itself can be collected and our list doesn’t grow huge without reason).

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

Sidebar

Related Questions

How do I keep a count of the number of times that objects of
I have a table and can count the number of rows using var count
I would like to know how I can count the number of unique values
In the 10 years I've been programming, I can count the number of data
How can I count the number of elements in an array, because contrary to
How can i count the number of stored procedures in my database and is
How can I count the number of files in a directory using C on
Is there a way I can programmatically count the number of links for a
I can not seem to figure out how to count the number of seconds
How can I display a counter that counts the number of times a file

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.