Search code examples
c#arrayspictureboxdispose

can I dispose a bitmap/picturebox array? C#


I wrote an algorithm to detect my crystals (circle shape) which have imperfect edges. Since,I don't know how many crystals will appear in my system I had to go for a safe estimation (which was 100 crystals). So, I defined pictureboxes array and bitmap arrays as follow:

    public PictureBox[] segment = new PictureBox[100];
    public Bitmap[] cropped = new Bitmap[100];

when the algorithm is done. I can run a loop to count the number of crystals (which equals to sum of non null member of cropped or segment array above). To make the program better I was wondering if I can Dispose/truncate these two arrays.


Solution

  • If you don't know what the final number of crystals will be, I'd suggest to use Collections (for example a list). So you don't have to count the values that are not null, you can simply take the length of the list. A list has a dynamic length. That means you don't have to declare a fixed length and you can add as many elements as you want (in theory).

    List<PictureBox> segment = new List<PictureBox>();
    List<Bitmap> cropped = new List<Bitmap>();
    

    See: http://www.dotnetperls.com/list

    But to return to your question: I assume that by assigning null as the values of the arrays, the .NET garbage collector will handle the reallocation of memory for you.

    See: http://msdn.microsoft.com/de-de/library/0xy59wtx(v=vs.110).aspx

    I'm sorry for giving you a wrong information, please refer to the following article: How to delete an array in c#?