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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:21:57+00:00 2026-06-15T02:21:57+00:00

I found an unusual sample in FCL code. This is method in System.IO.BinaryReader: protected

  • 0

I found an unusual sample in FCL code.

This is method in System.IO.BinaryReader:

    protected virtual void Dispose(bool disposing) { 
        if (disposing) { 
            Stream copyOfStream = m_stream;
            m_stream = null; 
            if (copyOfStream != null && !m_leaveOpen)
                copyOfStream.Close();
        }
        m_stream = null; 
        m_buffer = null;
        m_decoder = null; 
        m_charBytes = null; 
        m_singleChar = null;
        m_charBuffer = null; 
    }

What impact on the execution logic has ‘copyOfStream’?

  • 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-15T02:21:58+00:00Added an answer on June 15, 2026 at 2:21 am

    It’s done so that m_stream will be set to null even if Stream.Close() throws an exception.

    In response to comments

    what kind of exception can be raised on Stream.Close()

    When you close a stream, any buffered output is flushed, which can throw an exception, e.g. due to a network failure.

    I’m not sure that this is a rationale. Usually we’re calling Dispose in using block, and this means that variable will never used anyway

    In the above, if Stream.Close() throws, then m_stream will already be null. With the following alternative, m_stream won’t be set to null if Stream.Close() throws:

    m_stream.Close(); // <== throws
    m_stream = null;  // <== not executed if Close() throws
    

    But in this case if we’re talking about exception-safety, someone can retry call to Dispose method and will face NullReferenceException

    It won’t throw a NullReferenceException because it tests for null:

    if (copyOfStream != null && !m_leaveOpen)
            copyOfStream.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is Java pass-by-reference? I found an unusual Java method today: private void
found this little code snippet that seems to do what i want, but im
While looking on some legacy code, I found a rather unusual construction (for me
I found an unusual bug in my code recently through random ad-hoc testing. So,
In the Eclipse CDT plugin, I found this unusual way of initializing a field
Found this while reading the Neo4j manual, specifically here , I found the sentence:
Found This: tyty stack, Social Icons not working with Infinite Scrolling on Wordpress I'm
Found this Multimap containing pairs? , but it is not much help How would
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can

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.