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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:09:12+00:00 2026-05-23T20:09:12+00:00

I am following MSDN guideline of implementing a Dispose Method . I’ve written my

  • 0

I am following MSDN guideline of implementing a Dispose Method. I’ve written my simple code to better understand and run the code step by step.

EDITED: changed title to better fit the problem

This is the code:

class Program {
    static void Main(string[] args) {
        Base0 base0 = new Base0();
        base0.Dispose();

        Console.WriteLine();
        Sub1 sub1 = new Sub1();
        sub1.Dispose();

        Console.ReadLine();
    }
}
class Base0 : IDisposable {
    private bool disposed;
    public Base0() {
        Console.WriteLine("Creating Base0!");
        this.disposed = false;
        // allocating some resources
    }
    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    protected virtual void Dispose(bool disposing) {
        Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
        if (!this.disposed) {
            if (disposing) {
                // disposing all managed resources
            }
            // disposing all unmanaged resources
        }
    }
    public void DoSomething() {
        if (this.disposed) {
            throw new ObjectDisposedException(this.GetType().ToString());
        }
    }
    ~Base0() {
        Dispose(false);
    }
}
class Sub1 : Base0 {
    private bool disposed;
    public Sub1() {
        Console.WriteLine("Creating Sub1!");
        this.disposed = false;
        // allocating some resources
    }
    protected override void Dispose(bool disposing) {
        Console.WriteLine("Disposing " + this.GetType().ToString() + "!");
        if (!this.disposed) {
            try {
                if (disposing) {
                    // disposing all managed resources
                }
                // disposing all unmanaged resources
            }
            finally {
                base.Dispose(disposing);
            }
        }
    }
}

This is the output:

Creating Base0!  
Disposing DisposeFinalizeMethods.Base0!  

Creating Base0!  
Creating Sub1!  
Disposing DisposeFinalizeMethods.Sub1!  
Disposing DisposeFinalizeMethods.Sub1!

I am confused because I expected that the last line would be saying “Diposing … Base0!”, the base type.

The code executes as it should, I’ve checked it ‘step by step’ many times, I understand it but there is something that I’ve missed. What am I missing?

  • 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-23T20:09:12+00:00Added an answer on May 23, 2026 at 8:09 pm

    OK, this is not about Dispose or IDisposable but about GetType.

    this.GetType() is a call to a virtual method. When called in a base-class it will give the Type of the actual (derived) type.

    To reproduce:

    class A
    {
        public virtual void Print()
        {
            Console.Write(this.GetType().Name);
        }
    }
    
    class B : A
    {
        public override void Print()
        {
            base.Print();
            Console.Write(this.GetType().Name);
        }
    }
    
    
            var b = new B();
            b.Print();
    

    Will print BB.

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

Sidebar

Related Questions

the following code is from MSDN: Idisposable pattern protected virtual void Dispose(bool disposing) {
Why do the following documents talk about different approaches when implementing Equals method? [MSDN]
Following a MSDN web page, I am trying to manually run mstest within my
MSDN states the following SortedSet(T).Add Method : If Count is less than the capacity
Given the following MSDN sample code, why can't I define the Action delegate inline:
I came across the following code on msdn: unsafe static void SquarePtrParam (int* p)
I use the following MSDN code on my master page to detect if the
I am following a MSDN article to understand the single ton design pattern. I
I fail to understand the hex annotation at the MSDN site On the following
I have created a simple WCF service by following a MSDN tutorial. I successfully

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.