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 6582395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:16:28+00:00 2026-05-25T16:16:28+00:00

Is it legal to call a method on disposed object? If yes, why? In

  • 0

Is it legal to call a method on disposed object? If yes, why?

In the following demo program, I’ve a disposable class A (which implements IDisposable interface).As far as I know, if I pass disposable object to using() construct, then Dispose() method gets called automatically at the closing bracket:

A a = new A();
using (a)
{
   //...
}//<--------- a.Dispose() gets called here!

//here the object is supposed to be disposed, 
//and shouldn't be used, as far as I understand.

If that is correct, then please explain the output of this program:

public class A : IDisposable
{
   int i = 100;
   public void Dispose()
   {
      Console.WriteLine("Dispose() called");
   }
   public void f()
   {
      Console.WriteLine("{0}", i); i  *= 2;
   }
}

public class Test
{
        public static void Main()
        {
                A a = new A();
                Console.WriteLine("Before using()");
                a.f();
                using ( a) 
                {
                    Console.WriteLine("Inside using()");
                    a.f();
                }
                Console.WriteLine("After using()");
                a.f();
        }
}

Output (ideone):

Before using()
100
Inside using()
200
Dispose() called
After using()
400

How can I call f() on the disposed object a? Is this allowed? If yes, then why? If no, then why the above program doesn’t give exception at runtime?


I know that the popular construct of using using is this:

using (A a = new A())
{
   //working with a
}

But I’m just experimenting, that is why I wrote it differently.

  • 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-05-25T16:16:29+00:00Added an answer on May 25, 2026 at 4:16 pm

    Disposed doesn’t mean gone. Disposed only means that any unmanaged resource (like a file, connection of any kind, …) has been released. While this usually means that the object doesn’t provide any useful functionality, there might still be methods that don’t depend on that unmanaged resource and still work as usual.

    The Disposing mechanism exist as .net (and inheritly, C#.net) is a garbage-collected environment, meaning you aren’t responsable for memory management. However, the garbage collector can’t decide if an unmanaged resource has been finished using, thus you need to do this yourself.

    If you want methods to throw an exception after the object has been diposed, you’ll need a boolean to capture the dispose status, and once the object is disposed, you throw the exception:

    public class A : IDisposable
    {
       int i = 100;
       bool disposed = false;
       public void Dispose()
       {
          disposed = true;
          Console.WriteLine("Dispose() called");
       }
       public void f()
       {
          if(disposed)
            throw new ObjectDisposedException();
    
          Console.WriteLine("{0}", i); i  *= 2;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is the following code the case of legal forward referencing? if yes why? public
The Java documentation says: A class implements the Cloneable interface to indicate to the
Is it legal? class SomeClass { public: static void f(); }; using SomeClass::f; Edit:
A friend recently had the occasion for a legal name change, which made me
I have a class FourByFourBoard that extends GameBoard . I define the following fields:
Is the following code legal? char* randomMethod1() { char* ret = "hello"; return ret;
In Java, is it legal to call remove on a collection when iterating through
I have the following line in my program that causes a run-time warning: if
I have a class similar to the following: class SomeClass { public: template<typename... Args>
My program has a button which when clicked opens an openfiledialog to choose a

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.