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

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.

commented: Helpfull explanation! +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.