ratty
ratty

Reputation: 13444

How to dispose a property which declared as list?

I want to dispose my list which declared as a list of objects. please find the example

Class A
{
    public void m()
    {
        Ilist<myclass> sam = new Ilist<myclass>();
        sam.Add(ob1); ==> some objects i am adding
        sampleProperty = sam;
    }
}

class B:C
{
    public Ilist<myclass> sampleProperty
    {
        get;
        set;
    }

    public override void m()
    {
        foreach(myclass s in sampleProperty)
        {
        }
    }
}

After my application is closed, my property is still alive. I cant set it to null because I am using my property in an override method, so I don't know when it will be called.i forgot to inform one thing class C is implements IDisposable interface

Upvotes: 1

Views: 518

Answers (1)

Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

You don't need to dispose lists in C#. The automatic garbage collector does it for you. You only dispose objects when you need to free, release, or reset unmanaged resources.

EDIT: If its the case that myClass is disposable (implements IDisposable), then you simply call object.Dispose() on each of the objects.

Upvotes: 5

Related Questions