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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:07:50+00:00 2026-06-09T01:07:50+00:00

Recently I’ve seen some code written as follows: public void Dipose() { using(_myDisposableField) {

  • 0

Recently I’ve seen some code written as follows:

public void Dipose()
{
   using(_myDisposableField) { }
}

This seems pretty strange to me, I’d prefer to see myDisposableField.Dispose();

What reasons are there for using “using” to dispose your objects over explicitly making the call?

  • 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-09T01:07:53+00:00Added an answer on June 9, 2026 at 1:07 am

    No, none at all. It will just compile into an empty try/finally and end up calling Dispose.

    Remove it. You’ll make the code faster, more readable, and perhaps most importantly (as you continue reading below) more expressive in its intent.

    Update: they were being slightly clever, equivalent code needs a null check and as per Jon Skeet’s advice, also take a local copy if multi-threading is involved (in the same manner as the standard event invocation pattern to avoid a race between the null check and method call).

    IDisposable tmp = _myDisposableField; 
    
    if (tmp != null) 
        tmp.Dispose();
    

    From what I can see in the IL of a sample app I’ve written, it looks like you also need to treat _myDisposableField as IDisposable directly. This will be important if any type implements the IDisposable interface explicitly and also provides a public void Dispose() method at the same time.

    This code also doesn’t attempt to replicate the try-finally that exists when using using, but it is sort of assumed that this is deemed unnecessary. As Michael Graczyk points out in the comments, however, the use of the finally offers protection against exceptions, in particular the ThreadAbortException (which could occur at any point). That said, the window for this to actually happen in is very small.

    Although, I’d stake a fiver on the fact they did this not truly understanding what subtle “benefits” it gave them.

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

Sidebar

Related Questions

Recently, we discovered odd behavior in some old code. This code has worked for
Recently, I've discovered this code of the following structure: Interface : public interface Base<T>
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Recently downloaded some code for a minor open-source project related to a small webgame
Recently I found some simple source code of a bootloader.The following is the simple
recently, while working on a db2 -> oracle migration project, we came across this
Recently, I was asked to compile some of my Android projects, based on the
recently I started using storyboard and I've the following situation: I want to set
Recently I've begun to code in Objective-C for iOS 5 devices. My brand new
Recently we have released our product. Our team decided to preserve the source code

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.