C# 计算类的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12276641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:43:32  来源:igfitidea点击:

Count instances of the class

c#.netclassobject

提问by Tomas

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

可能的重复:
我怎样才能找出在 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.

应该返回 2。如果 GC 销毁了这些类中的任何一个,则返回 1 或 0。

采纳答案by Dor Cohen

You can holds global static counter in your program.
This is a simple thread safe solution:

您可以在程序中保存全局静态计数器。
这是一个简单的线程安全解决方案:

class MyClass
{
    static int counter = 0;

    public MyClass()
    {
        Interlocked.Increment(ref counter);
    }

    ~MyClass()
    {
        Interlocked.Decrement(ref counter);
    }
}

also take a look at the following similar question - Count number of objects of class type within class method

也看看下面的类似问题 -在类方法中计算类类型的对象数

回答by Jon

Only if you implement a counting mechanism inside the constructor (increment) and finalizer (decrement). But even that will not account for instances which are really inactive (noone has any reference to them) but have not been collected yet.

仅当您在构造函数(增量)和终结器(减量)中实现计数机制时。但即使这样也不会考虑那些真正不活动(没有人提到它们)但尚未收集的实例。

Moreover, adding a finalizer to a class -- no matter how trivial -- will adversely affect performance, which is an argument against doing so.

此外,向类添加终结器——无论多么微不足道——都会对性能产生不利影响,这是反对这样做的论据。

回答by Esben Skov Pedersen

public class MyClass
{
    private static int count;
    private static object _lock = new object();

    public MyClass()
    {
         lock(_lock)
         {
             count++;
         }
     }

    private ~MyClass()
    {
        lock(_lock)
        {
             count--;
        }
    }
}

回答by Can Poyrazo?lu

I don't know of a built in mechanism, but you can always incrase a private static variable in constructor.

我不知道内置机制,但是您始终可以在构造函数中增加私有静态变量。

public class MyClass
{
    private static int instances = 0;

    public MyClass() => instances++;
    ~MyClass() => instances--;
}

Haven't tried but should work.

没试过,但应该可以。

回答by SMK

You can try it by making a static variable for count in class and increment it in constructorand decrement in destructor.May this helps you.

您可以通过在类中创建一个用于计数的静态变量来尝试它,constructor并在destructor.

回答by Adil Mammadov

Try this one:

试试这个:

public class MyClass
{
    public static int activeCount = 0;

    public MyClass() => activeCount++;
    ~MyClass() => activeCount--;
}


//In the main
var testClass1 = new MyClass();
var testClass2 = new MyClass();

Console.WriteLine(MyClass.activeCount);

回答by Pranay Rana

Such thing is not possible but you can do something like

这种事情是不可能的,但你可以做类似的事情

Note :ClassInstancecan be also int value just to maintain count.

注意:ClassInstance也可以是 int 值只是为了保持计数。

public class MyType 
{
    public static List<MyType> ClassInstance = new List<MyType>();

    public MyType() => ClassInstance.Add(this);
    public RemoveClass(MyType t)
    {
        ClassInstance.Remove(t);
        t = null;
    }

    public int ActiveCount => ClassInstance.Count;
}

回答by Gyan Chandra Srivastava

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

simple and easy get instance active by countinstance

简单轻松地通过计数实例激活实例

回答by Habib Zare

this :

这个 :

public class MyClass
{
    private static int instances = 0;

    public MyClass()
    {
        instances++;
    }

    ~MyClass()
    {
        instances--;
    }


    public static int GetActiveInstances()
    {
        return instances;
    }

}

use :

用 :

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

     int count = MyClass.GetActiveInstances();