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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:59:00+00:00 2026-05-27T01:59:00+00:00

While crawling through sources on the web, I’ve encountered a lot of boiletplate code

  • 0

While crawling through sources on the web, I’ve encountered a lot of boiletplate code which looks the following way:

Suppose we have some public class CustomObject: IDisposable, which has a bunch of methods.

Now, each of those methods has default sanity checks:

if (inputBuffer == null)
    throw new ArgumentNullException("inputBuffer");
if (outputBuffer == null)
    throw new ArgumentNullException("outputBuffer");
if (inputCount < 0)
    throw new ArgumentException("inputCount", "< 0");

But (due to the IDisposable interface implementation) the following check is added to each method:

if (disposed)
    throw new ObjectDisposedException("MethodName");

Now – is this a common practice? Should I start reengineering my old disposable classes and implementing these checks?

  • 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-27T01:59:00+00:00Added an answer on May 27, 2026 at 1:59 am

    It depends upon your usage, if in doubt it doesn’t hurt to add it.

    For a class intended to be used by other programs (e.g., libraries and frameworks) I would always perform this check and throw the correct exception as this will aid other applications in diagnosing errors and makes the class more robust.

    For internal classes only intended to be consumed by my application you can skip the check if the error will surface quickly when calling the methods. E.g. if every method in the class used a stream, and that stream was disposed or set to null it would result in an exception pretty quickly.

    If the internal class has methods that won’t error though, I will always use an explicit check as I don’t want a situation where some of the methods still work after the object has been disposed (except for methods that explicitly allow it such as IsDisposed).

    Having the explicit check does have the advantage of explicitly documenting which methods are allowed to be called after the object has been disposed. More so if you add a comment at the top of methods that do not call GuardDisposed to state that it is allowed, then any method that doesn’t start with GuardDisposed or a comment can be regarded as suspect.

    To actually implement the check I prefer to move it to a separate method and use it like an assertion, e.g.

    public class Foo
    {
        private bool disposed;
    
        public void DoSomething ()
        {
            GuardDisposed ();
        }
    
        protected void GuardDisposed ()
        {
            if (disposed)
                throw new ObjectDisposedException (GetType ().Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to compile the following code, which is enhanced version of read build
While validating my website's HTML code in the W3C validator I got the following
While going through university and from following the development of SO, I've heard a
While browsing through the source code of the java.util.concurrency package, I noticed an idiomatic
While reading through another question here, on creating a URL shortening service, it was
While doing some JavaScript performance tests I came up with the following piece of
while($c=fgets(STDIN)){ if($c===PHP_EOL){ continue; } else { echo $c; } } When the above code
While programming with python I am often confused with the following ambiguity: should it
While working on Hadoop Implementation in Pseudo-Distributed Operation, I found following exception of JAVA_HOME
This is the below code in my MyCrawler.java and it is crawling all those

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.