How to delete an instance of a class?

2019-02-25 12:29发布

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 ...

标签: c# class flush
5条回答
Rolldiameter
2楼-- · 2019-02-25 13:04

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

查看更多
贪生不怕死
3楼-- · 2019-02-25 13:05

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

查看更多
够拽才男人
4楼-- · 2019-02-25 13:11

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

查看更多
该账号已被封号
5楼-- · 2019-02-25 13:17

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);
    }
}
查看更多
Lonely孤独者°
6楼-- · 2019-02-25 13:18

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.

查看更多
登录 后发表回答