How do I repeat class instances in C #?

Is there a way to iterate over instances of a class in C #? These instances are not tracked or managed in the collection.

+4
source share
5 answers

Not inside a regular structure. You will need to track them manually.

You can, however, do this in windbg / sos - mainly for debugging purposes (not for regular code).

+8
source

You should have links to them somewhere, or at least know where to look, so by identifying them, you probably put them in a collection that you then repeat.

If you do not know where the links live, you will need to introduce some kind of tracking mechanism. Maybe a static collection by type? However, this must be carefully implemented.

+1
source

Not directly.

You could conceptually place your object in some known place (for example, a static collection) and then use it for iteration, but then you will need to make sure that you deleted the instance from this collection at some point, otherwise it will never won't get garbage collection.

+1
source

As Mark said, if you want to do this in code, you will need to keep their collection. If you're debugging, take a look at this blog post: http://blogs.msdn.com/tess/archive/2006/01/23/516139.aspx

If you need a collection in memory of all instances of an object of a certain type, you can consider using the System.WeakRef collection. A ref weak link is a link that does not support the object to which it refers. This will allow you to keep a collection of weak references for instances of the objects you want to list. Take a look at Weakrefs in the Help for more information.

0
source

There is an interesting discussion and solution related to this issue in the comment thread in this post .

0
source

All Articles