Darrin Lynn
Darrin Lynn

Reputation: 158

Do I need to dispose of a MessageBox or will it take care of itself?

Do I need to dispose of a MessageBox or will it take care of itself?

I have the line of code:

MessageBox.Show(
    message, 
    title, 
    MessageBoxButtons.OK, 
    MessageBoxIcon.Information);

When the user hits the OK button and the dialog box goes away is it removed from memory?

Upvotes: 3

Views: 2128

Answers (4)

MGZero
MGZero

Reputation: 5963

Nothing to worry about here. First off, you can't call dispose() on it because it isn't disposable. Second, you didn't instantiate the class (you called a static method) so there's nothing for you to really dispose of anyway.

Upvotes: 1

Guffa
Guffa

Reputation: 700552

You don't need to dispose of a MessageBox.

In fact it's not even possible to dispose a MessageBox, as it's not possible to create an instance of the class.

"You cannot create a new instance of the MessageBox class."

http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

Upvotes: 1

Uwe Keim
Uwe Keim

Reputation: 40736

The MessageBox class does not implement the IDisposable interface, so you cannot dispose an instance.

Plus, as in your example, you are calling a static method, so there is no instance to dispose anyway.

Upvotes: 10

Joshua Honig
Joshua Honig

Reputation: 13215

It is removed from memory...eventually. More importantly, you do not have to worry or think about it. See Garbage Collection.

Upvotes: 2

Related Questions