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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:24:13+00:00 2026-05-17T22:24:13+00:00

As describe this article , about usage of using on IDisposable objects, it says

  • 0

As describe this article, about usage of using on IDisposable objects, it says one interesting words:

…using block, the Dispose method is automatically called sometime after the block ends. (It may not be immediate; it depends on the CLR.)

Interesting here is “It may not be immediate; it depends on the CLR“.
Can anyone provide more details on this? Because we have some strange situations where it seems that on code using(new MyDisposable()) {…}, after end of block } it does NOT immediately calls Dispose method on MyDisposable instance, but some time later.

UPDATE: Conclusion for me, it seems to me that i have problem elsewhere. I think that Dispose method can be called some time later after using block ends. But when it is not like that, than i must find problem somewhere else in my code.
Thanks for responses!

  • 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-17T22:24:13+00:00Added an answer on May 17, 2026 at 10:24 pm

    I’m a little skeptical of that statement, and think they meant something else (perhaps garbage collection). A using statement is just syntactic sugar for a try/finally block where the finally block calls dispose. Given this C#:

    using (var fs = new FileStream("C:\\blah.txt", FileMode.CreateNew))
    {
        fs.WriteByte(7);
    }
    

    The IL looks like this:

    //snipped
    L_000e: nop 
    L_000f: ldstr "C:\\blah.txt"
    L_0014: ldc.i4.1 
    L_0015: newobj instance void [mscorlib]System.IO.FileStream::.ctor(string, valuetype [mscorlib]System.IO.FileMode)
    L_001a: stloc.0 
    L_001b: nop 
    L_001c: ldloc.0 
    L_001d: ldc.i4.7 
    L_001e: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
    L_0023: nop 
    L_0024: nop 
    L_0025: leave.s L_0037
    L_0027: ldloc.0 
    L_0028: ldnull 
    L_0029: ceq 
    L_002b: stloc.1 
    L_002c: ldloc.1 
    L_002d: brtrue.s L_0036
    L_002f: ldloc.0 
    L_0030: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    L_0035: nop 
    L_0036: endfinally 
    L_0037: nop 
    L_0038: nop 
    L_0039: ret 
    .try L_001b to L_0027 finally handler L_0027 to L_0037
    

    Notice on the last line it’s just a .try and .finally. This is also indicated in The using statement from the C# spec.

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

Sidebar

Related Questions

No related questions found

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.