Alex Rivera | Logout

Count instances of the class

Asked 2012-09-05T07:37:23.943
17

Possible Duplicate:
how can i find out how many objects are created of a class in C#

Is it possible to get number of instances which is active(created and not yet destroyed) for selected class?

For example:

public class MyClass { }

...

var c1 = new MyClass();
var c2 = new MyClass();

count = GetActiveInstances(typeof(MyClass))

Should return 2. If GC destroy any of these classes then 1 or 0.

Edit
Report

1 Answer

1
 public class MyClass
    {
public  static int countinstance  =0;
MyClass(){ countinstance  ++;}
 ~ MyClass() {countinstance  --; }
    }

simple and easy get instance active by countinstance

answered 2012-09-05T07:44:13.743

Your Answer