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

  • Home
  • SEARCH
  • 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 914639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:45:01+00:00 2026-05-15T17:45:01+00:00

I am working on a class that deals with a lot of Sql objects

  • 0

I am working on a class that deals with a lot of Sql objects – Connection, Command, DataAdapter, CommandBuilder, etc. There are multiple instances where we have code like this:

if( command != null )
{
    command.Dispose();
}

if( dataAdapter != null )
{
    dataAdapter.Dispose();
}

etc

I know this is fairly insufficient in terms of duplication, but it has started smelling. The reason why I think it smells is because in some instances the object is also set to null.

if( command != null )
{
    command.Dispose();
    command = null;
}

I would love to get rid of the duplication if possible. I have come up with this generic method to dispose of an object and set it to null.

private void DisposeObject<TDisposable>( ref TDisposable disposableObject )
    where TDisposable : class, IDisposable
{
    if( disposableObject != null )
    {
        disposableObject.Dispose();
        disposableObject = null;
    }
}

My questions are…

  1. Is this generic function a bad idea?
  2. Is it necessary to set the object to null?

EDIT:

I am aware of the using statement, however I cannot always use it because I have some member variables that need to persist longer than one call. For example the connection and transaction objects.

Thanks!

  • 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-15T17:45:01+00:00Added an answer on May 15, 2026 at 5:45 pm

    I assume these are fields and not local variables, hence why the using keyword doesn’t make sense.

    Is this generic function a bad idea?

    I think it’s a good idea, and I’ve used a similar function a few times; +1 for making it generic.

    Is it necessary to set the object to null?

    Technically an object should allow multiple calls to its Dispose method. (For instance, this happens if an object is resurrected during finalization.) In practice, it’s up to you whether you trust the authors of these classes or whether you want to code defensively. Personally, I check for null, then set references to null afterwards.

    Edit: If this code is inside your own object’s Dispose method then failing to set references to null won’t leak memory. Instead, it’s handy as a defence against double disposal.

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

Sidebar

Related Questions

I'm currently working on a class that calculates the difference between two objects. I'm
I'm working on a class that inherits from another class, but I'm getting a
I'm working on a class that is storing a 2D array of class MyType
So I'm working on this class that's supposed to request help documentation from a
I'm working on a sparse matrix class that needs to use an array of
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I am working in a project that has two main parts: a class library
I am working on a class that maintains a dictionary of images. This dictionary
I am working on a class that will implement a client for a certain
So, I am working on this class that has a few static constants: public

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.