none
Dispose of a generic list

    Question

  • Hi,

    I have a procedure in witch I create a new generic list of a structure.

    Example:
    List<myStruct> = new List<myStruct>();

    I add some elements to the list and at the end, I want to dispose of the generic list to free the memory. How do I proceed?

    There is no Dispose() method in a generic list.

    Thanks!
    Tuesday, June 21, 2005 3:55 PM

Answers

  • Simply let the variable go out of scope or set the member or static variable to null and the garbage collector will reclaim the memory. For instance,

    public void DoSomething() {
      List<MyStruct> myList = new List<MyStruct>();
      // Do stuff
    } // <-- myList is out of scope and will be cleaned up next time the GC runs

    private List<MyStruct> myOtherList;

    public void DoSomethingElse() {
      myOtherList = new List<MyStruct>();
      // Do stuff
    } // <-- myOtherList still exists

    public void CleanupStuff() {
      myOtherList = null; // <-- List<> object will be cleaned up next time the GC runs
    }
    Tuesday, June 21, 2005 4:01 PM

All replies

  • Simply let the variable go out of scope or set the member or static variable to null and the garbage collector will reclaim the memory. For instance,

    public void DoSomething() {
      List<MyStruct> myList = new List<MyStruct>();
      // Do stuff
    } // <-- myList is out of scope and will be cleaned up next time the GC runs

    private List<MyStruct> myOtherList;

    public void DoSomethingElse() {
      myOtherList = new List<MyStruct>();
      // Do stuff
    } // <-- myOtherList still exists

    public void CleanupStuff() {
      myOtherList = null; // <-- List<> object will be cleaned up next time the GC runs
    }
    Tuesday, June 21, 2005 4:01 PM
  • Thank you :)
    Tuesday, June 21, 2005 4:40 PM