Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Anonymous instance implementing IDisposable interface

Anonymous instance implementing IDisposable interface

Scheduled Pinned Locked Moved C#
comtoolstutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Spacix One
    wrote on last edited by
    #1

    Today I found myself writing / reading binary image files, and I caught myself writing the following on a read:

    using(fileReader = new BinaryReader(new FileStream(filePathName, FileMode.Open)))
    {
    //process the file here
    }

    Before I corrected my self, it got me thinking what exactly happens to anonymous instance members that are implementing the IDisposable interface? Would it be lost after the child instance is the finalized by sitting and waiting to be Garbage Collected? Or in the above example would the FileStream be picked up with the BinaryReader? I do know that if you finalize the parent an instance is based off of, both get finalized... I know not as to what happens above??? :( This might even be a good article, if you could find it among the 1000's of hits on this topics keywords. plz send teh codez? :~


    -Spacix All your skynet questions[^] belong to solved


    I dislike the black-and-white voting system on questions/answers. X|


    N 1 Reply Last reply
    0
    • S Spacix One

      Today I found myself writing / reading binary image files, and I caught myself writing the following on a read:

      using(fileReader = new BinaryReader(new FileStream(filePathName, FileMode.Open)))
      {
      //process the file here
      }

      Before I corrected my self, it got me thinking what exactly happens to anonymous instance members that are implementing the IDisposable interface? Would it be lost after the child instance is the finalized by sitting and waiting to be Garbage Collected? Or in the above example would the FileStream be picked up with the BinaryReader? I do know that if you finalize the parent an instance is based off of, both get finalized... I know not as to what happens above??? :( This might even be a good article, if you could find it among the 1000's of hits on this topics keywords. plz send teh codez? :~


      -Spacix All your skynet questions[^] belong to solved


      I dislike the black-and-white voting system on questions/answers. X|


      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      This is interesting. I got the following when I looked BinaryReader class through reflector

      protected virtual void Dispose(bool disposing)
      {
      if (disposing)
      {
      Stream stream = this.m_stream;
      this.m_stream = null;
      if (stream != null)
      {
      stream.Close();
      }

      }
      this.m_stream = null;
      this.m_buffer = null;
      this.m_decoder = null;
      this.m_charBytes = null;
      this.m_singleChar = null;
      this.m_charBuffer = null;
      }

      Noticed the lines in bold ? it's closing the associated stream but not disposing. Also I noticed BinaryReader don't have a destructor. Any thoughts ?

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      S 1 Reply Last reply
      0
      • N N a v a n e e t h

        This is interesting. I got the following when I looked BinaryReader class through reflector

        protected virtual void Dispose(bool disposing)
        {
        if (disposing)
        {
        Stream stream = this.m_stream;
        this.m_stream = null;
        if (stream != null)
        {
        stream.Close();
        }

        }
        this.m_stream = null;
        this.m_buffer = null;
        this.m_decoder = null;
        this.m_charBytes = null;
        this.m_singleChar = null;
        this.m_charBuffer = null;
        }

        Noticed the lines in bold ? it's closing the associated stream but not disposing. Also I noticed BinaryReader don't have a destructor. Any thoughts ?

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        S Offline
        S Offline
        Spacix One
        wrote on last edited by
        #3

        This is odd, why does did MS even give it an IDisposable interface? Seams that streams don't need finalization and thusly just calling close is all you need.


        -Spacix All your skynet questions[^] belong to solved


        I dislike the black-and-white voting system on questions/answers. X|


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups