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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:17:11+00:00 2026-06-04T19:17:11+00:00

Why do we need parameter disposing in the below code snippet. Moreover we invoke

  • 0

Why do we need parameter disposing in the below code snippet.

Moreover we invoke dispose with false, in finalizer and it won’t release or do the clean up.

So what if dispose never get called?

Isn’t dispose always get called before finalizer?

using System;
public class MyClass : IDisposable 
{ 
    private bool disposed = false;  
    protected virtual void Dispose(bool disposing) 
    {    
        if (!disposed)
        {
            **//Do we really need this condition?
            if (disposing)**
            { 
                // called via myClass.Dispose().    
                // OK to use any private object references
            }
            disposed = true; 
        } 
    }
    public void Dispose() 
        // Implement IDisposable     
    {
        Dispose(true);   
        GC.SuppressFinalize(this); 
    } 
    ~MyClass() // the finalizer
    {
        //why do we need to call with false?
        Dispose(false);    
    }
} 

In other words, why not?

using System;
public class MyClass : IDisposable 
{ 
    private bool disposed = false;  
    protected virtual void Dispose(bool suppressFinalize) 
    {    
        if (!disposed)
        {
            //Do we really need this condition?

                // called via myClass.Dispose().    
                // OK to use any private object references            
            disposed = true; 
        }
        if (!suppressFinalize)
        {
            GC.SuppressFinalize(this); 
        }
    }
    public void Dispose() 
        // Implement IDisposable     
    {
        Dispose(true);   

    } 
    ~MyClass() // the finalizer
    {
        //why do we need to call with false?
        Dispose(false);    
    }
} 

In fact, do I really need finalizer? Why not this?

using System;
public class MyClass : IDisposable 
{     
    public void Dispose() 
        // Implement IDisposable     
    {
        //just do the cleanup and release resources
        GC.SuppressFinalize(this);
    } 

} 
  • 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-04T19:17:13+00:00Added an answer on June 4, 2026 at 7:17 pm

    Moreover we invoke dispose with false, in finalizer and it wont release or do the clean up.

    Indeed – it will assume that other classes handle their own cleanup in that case, and only do the clean-up of direct unmanaged resources.

    So what if dispose never get called?

    Then the finalizer will be called, and it will clean up any direct unmanaged resources, but not worry about indirect resources.

    Isn’t dispose always get called before finalizer?

    Not if no-one calls it for whatever reason.

    I think this pattern is more complicated than it needs to be, as it’s trying to account for classes which act as base classes for other classes which might need finalizers. Seal your classes and you can just implement exactly what you’d expect to 🙂

    You might also want to read Joe Duffy’s “Never write a finalizer again” blog post and the long explanation of the pattern.

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

Sidebar

Related Questions

sample1.php?q=+str This code is for sending single parameter i need to send multiple variables
If it is possible, how does the parameter need to look like in the
I am sending various parameters to batch file. In that i need the parameter
I need to get parameter that is passed in URL with parameter name ONum
I need to print a received parameter at the end of every line... Here
I need to send a string as parameter in UIButton selector. Taking global string
I need to dynamically change the loaded keys through a parameter. Imagine there yould
I need to write the expression meaning optimize over the parameter set . I
I have an object parameter and I need to check if the object implements
I need to write a generic class where the type parameter must be something

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.