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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:14:59+00:00 2026-05-14T04:14:59+00:00

In most examples that you find on the web when explicitly not using using,

  • 0

In most examples that you find on the web when explicitly not using “using”, the pattern looks something like:

  SqlConnection c = new SqlConnection(@"...");
  try {
    c.Open();
  ...
  } finally {
    if (c != null) //<== check for null
      c.Dispose();
  }

If you do use “using” and look at the generated IL code, you can see that it generates the check for null

L_0024: ldloc.1 
L_0025: ldnull 
L_0026: ceq 
L_0028: stloc.s CS$4$0000
L_002a: ldloc.s CS$4$0000
L_002c: brtrue.s L_0035
L_002e: ldloc.1 
L_002f: callvirt instance void [mscorlib]System.IDisposable::Dispose()
L_0034: nop 
L_0035: endfinally 

I understand why the IL gets translated to check for null (doesn’t know what you did inside the using block), but if you’re using try..finally and you have full control of how the IDisposable object gets used inside the try..finally block, do you really need to check for null? if so, why?

  • 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-14T04:15:00+00:00Added an answer on May 14, 2026 at 4:15 am

    “using” statements can initialize variables with calls other than constructors. For example:

    using (Foo f = GetFoo())
    {
        ...
    }
    

    Here f could easily be null – whereas a constructor call can never1 return null. That’s why a using statement checks for nullity. It’s not to do with what’s inside the block itself, because the using statement preserves the original initial value. If you write:

    Stream s;
    using (s = File.OpenRead("foo.txt"))
    {
        s = null;
    }
    

    then the stream will still be disposed. (If the variable is declared in the initializer part of the using statement, it’s read-only anyway.)

    In your case, as you know that c is non-null before you enter the try block, you don’t need to check for null in the finally block unless you’re reassigning its value (which I sincerely hope you’re not!) within the block.

    Now with your current code there is the slight risk that an asynchronous exception could be thrown between the assignment to c and entering the try block – but it’s hard to avoid this sort of race condition completely, as there could equally be an asynchronous exception after the constructor has completed but before the value is assigned to c at all. I would suggest that most developers don’t need to worry about this sort of thing – asynchronous exceptions tend to be sufficiently “hard” that they’ll bring down the process anyway.

    Is there any reason you don’t want to just use a using statement anyway though? To be honest, I very rarely write my own finally blocks these days…


    1 See Marc’s answer and weep. Not usually relevant though.

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

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SQLQueryStress - http://www.datamanipulation.net/SQLQueryStress/documentation/documentation.asp and SQLStress - http://www.sqlstress.com/Overview.aspx Both look like… May 14, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer PHP only has function scope - control structures such as… May 14, 2026 at 11:27 pm
  • Editorial Team
    Editorial Team added an answer Here's the correct way to use this method: NSDictionary *myDictionary… May 14, 2026 at 11:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.