How to delete an instance of a class?

眉间皱痕 提交于 2019-12-04 04:31:58

问题


So, I have this project that creates multiple instances of a class, and list them.

At some point, the instanced class is not needed anymore. How can I flush it ?

So far, it doesn't happen, even when the class has nothing to do anymore (if it had been a single static class, the program would have shut down), it's still in my list, and its public variables are still available ...


回答1:


You can have your instances raise an event (OnInactivated, say). The list class should add a handler for such events when the item is added to the list (subclass or encapsulate a list and override the Add method). When the event fires, the object will be removed from the list by the list. Assuming the object is not referenced in any other lists (which might also be listening), then the garbage collector is free to deal with the object.

In this way, the object does not need to keep track of all references to itself, and know how to inform the reference holder to release the reference.

Here's a runnable example: http://www.coderun.com/ide/?w=Hw83i0wAQkugUif5Oj2lmw




回答2:


Your question implies that you have these instances in a list of some kind. Remove the instances you no longer want from the list, making sure you don't have any other references to them. At some point, the garbage collector will reclaim them.




回答3:


Why the concern to flush it? Why not just let .Net Garbage Collection do it's thing?

Altenatively you could implement IDisposable and call

MyClass.Dispose();

UPDATE: Sounds like you want a custom collection, not a list, that will only return active classes. Create a new class called MyClassCollection that implements ICollection and make sure it only ever returns active classes




回答4:


Example to hopefully explain what should happen:

public class MyClass {
    public string MyString { get; private set; }
    public int MyInt { get; private set; }
    public double MyDouble { get; private set; }

    public MyClass(string myString, int myInt, double myDouble){
        MyString = myString;
        MyInt = myInt;
        MyDouble = myDouble;
    }
}

public class SomeOtherClass{
    public List<MyClass> m_Instances = new List<MyClass>();

    public void FillList(){
        //Instantiate 3 items, and add to the list
        m_Instances.Add(new MyClass("1", 2, 3d));
        m_Instances.Add(new MyClass("4", 5, 6d));
        m_Instances.Add(new MyClass("7", 8, 9d));
    }

    public void RemoveFirst(){
        //Remove the first one. As long as the removed item has no
        //other instances, the Garbage Collector will (in its own time)
        //destroy that unused object, and reclaim the memory.
        m_Instances.RemoveAt(0);
    }
}



回答5:


A more general solution to referencing objects without preventing their garbage collection is to use a WeakReference.



来源:https://stackoverflow.com/questions/3077961/how-to-delete-an-instance-of-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!