Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:58:55+00:00 2026-06-11T11:58:55+00:00

I have the following code in my program: //Dialog Form Class public int Age;

  • 0

I have the following code in my program:

//Dialog Form Class

public int Age;

private void goButtonClick(object sender, EventArgs e)
{
     Age = trackBar1.Value;
     Close();
}

//Main Form Class

DialogForm df = new DialogForm();
df.ShowDialog();
df.Dispose();
if(df.Age >= 18)
{
    //do stuff
}

Surprisingly, I thought I was going to need a way to access df.Age since it would have been disposed by the Close method but surprisingly, I could access this value. I assumed this was some kind of intelligence of the garbage collector so I added df.Dispose() before calling df.Age just to see what will happen, but I could still access it. So I got confused? Why is this happening? When exactly does an item get disposed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:58:56+00:00Added an answer on June 11, 2026 at 11:58 am

    Usually, the answer is “when your code calls .Dispose()“, which usually means “when it leaves the using block”, however some code has additional things that cause it to be disposed. For example, on a winform, if you use the Show() method to display it, then it is disposed when the form is closed.

    HOWEVER! For a form shown via ShowDialog(), this is not done; after all, it is modal, so the expected lifetime is obvious:

    using(var df = new DialogForm())
    {
        df.ShowDialog();
        if(df.Age >= 18)
        {
            //do stuff
        }
    }
    

    or better:

    int age;
    using(var df = new DialogForm())
    {
        df.ShowDialog();
        age = df.Age;
    }
    if(age >= 18)
    {
        //do stuff
    }
    

    You might also want to check the return value of ShowDialog() to see whether it was cancelled etc.

    But to answer your question directly: the form in your question is never properly disposed. The IDisposable.Dispose() method is never called.

    The garbage collector will find it at some point, and will call the finalizer, which will call the inbuilt Dispose(bool) pattern, but that is an implementation detail of winforms, and is not proper disposal.

    See also MSDN for ShowDialog():

    When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
i have written the following code class Program { static void Main(string[] args) {
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have the following code: class Program { static void Main(string[] args) { using
I have the following code: class Program { static void Main(string[] args) { Test
Hi I have following test code: class Program { static void Main(string[] args) {
I have the following code Snippet. class Program { static void Main(string[] args) {
I have a problem with the following code : class Program { public static
I have the following code: class Program { static void Main(string[] args) { string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.