0

Hi,

I have declared a Multi dimensional Array in C# like this:

private System.Windows.Forms.Button[,] ButtonArray = null;

Now after inserting into the array, I would like to re-initialize and empty the "ButtonArray"?

Please let me know the possible ways to do that

Thanks in advance for helping out a newbie. :)

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by DdoubleD
2

Once you remove all the references to your button objects stored in your array, the garbage collector will take care of the cleanup. You can then reallocate (reuse) your array for further usage.

// step 1 allocate...
            ButtonArray = new Button[10, 100];

            // step 2 remove reference to button objects; garbage collector will handle cleanup...
            ButtonArray = null;

            // step 3 allocate...
            ButtonArray = new Button[2, 5];

In the above, you could skip step 2 and perform step 3 because it will have the same affect of removing the references to the original button objects...

If any other existing objects reference any of the buttons, they will exist until all references have been remove--this is expected and desired.

Votes + Comments
Helpfull explanation!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.