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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:46:18+00:00 2026-05-27T15:46:18+00:00

I know that you can call an instance method that executes for each object.

  • 0

I know that you can call an instance method that executes for each object. I also know that you can have a static method on the type that is callable from the type.

But how would one call a method that acts on every instance of a particular type (say, to set a member variable to zero, for example)?

  • 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-27T15:46:19+00:00Added an answer on May 27, 2026 at 3:46 pm

    C# doesn’t provide a direct mechanism to track all reachable objects and there’s almost never a good reason to want such automatic tracking functionality (rather than, say, an explicit pool that you manage yourself).

    But to answer your requirement directly, you’ll need to:

    1. Manually track all the reachable instances of the type (perhaps with a set of weak-references to prevent the tracker itself from extending the objects’ lifetimes).
    2. Provide a mechanism to (say) perform a side-effect on each member of this set.
    3. Get rid of the associated weak-reference from the set when an object is destroyed. This is necessary to prevent a weak-reference leak.

    All of this will have to be done in a thread-safe manner.

    public class Foo
    {
        private static readonly HashSet<WeakReference> _trackedFoos = new HashSet<WeakReference>();
        private static readonly object _foosLocker = new object();
    
        private readonly WeakReference _weakReferenceToThis;
    
        public static void DoForAllFoos(Action<Foo> action)
        {
            if (action == null)
                throw new ArgumentNullException("action");
    
            lock (_foosLocker)
            {
                foreach (var foo in _trackedFoos.Select(w => w.Target).OfType<Foo>())
                    action(foo);
            }
        }
    
        public Foo()
        {
           _weakReferenceToThis = new WeakReference(this);
    
            lock (_foosLocker)
            {
                _trackedFoos.Add(_weakReferenceToThis);
            }
        }
    
        ~Foo()
        {
            lock (_foosLocker)
            {
                _trackedFoos.Remove(_weakReferenceToThis);
            }
        }
    }
    

    Are you sure you need all of this though? This is all really strange and non-deterministic (will be heavily impacted by when garbage-collection occurs).

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

Sidebar

Related Questions

I know that I can dynamically add an instance method to an object by
Ok so I know that you can't have an abstract static method, although I
(Yes I know I can call Java code from Scala; but that is pointless;
I have a silverlight client that can call a method on my WCF web
I know that I can pass object values through a URL pattern and use
we know that with the help of reflection we can create instance of a
I know that can co-exists with web form no problem as Hanselman mention here
I know that it's a subject that can raise a lot of debate, but
I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way

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.