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

Dani AI

Generated

For the specific case asked by the core choices are: replace the array reference, clear the existing array’s elements, or explicitly remove/dispose the Button controls if they were added to a UI container. is right that dropping references makes objects eligible for GC, but with WinForms Buttons you often need an extra step: remove them from their parent Controls collection and call Dispose so unmanaged resources are freed and there are no lingering references (event handlers can also keep objects alive).

If you want to keep the same dimensions and simply null out every slot without reallocating:

if (ButtonArray != null)
{
    Array.Clear(ButtonArray, 0, ButtonArray.Length);
}

Note: Array.Clear only sets references to null. It does not remove controls from the Form/Panel nor call Dispose.

If those Buttons were added to a container or you attached event handlers, do this before clearing (detach events, remove from parent, dispose, then null the array entry):

for (int r = 0; r < ButtonArray.GetLength(0); r++)
{
    for (int c = 0; c < ButtonArray.GetLength(1); c++)
    {
        var btn = ButtonArray[r, c];
        if (btn == null) continue;
        btn.Click -= YourClickHandler;            // detach events you attached
        if (btn.Parent != null) btn.Parent.Controls.Remove(btn);
        btn.Dispose();                             // free unmanaged resources
        ButtonArray[r, c] = null;
    }
}

If you don’t need fixed-size arrays, consider using collections (List<T> or jagged arrays) for easier resizing and removal. Summary: reassigning or setting the array variable to null is fine if no other references exist; for UI controls explicitly remove them from parents and Dispose (and detach handlers) before clearing references to avoid leaks.

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.