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. Should I use obj = null?

Should I use obj = null?

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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.
  • X Offline
    X Offline
    xkx32
    wrote on last edited by
    #1

    Hi guys I'm wondering if I should set the objects that I create to null after I'm done with it... let's say I create FileStream object in private method, after I close the object should I write: FileStreamObj.close(); FileStreamObj = null; or: FileStreamObj.close(); Is enough? TIA

    M E C 3 Replies Last reply
    0
    • X xkx32

      Hi guys I'm wondering if I should set the objects that I create to null after I'm done with it... let's say I create FileStream object in private method, after I close the object should I write: FileStreamObj.close(); FileStreamObj = null; or: FileStreamObj.close(); Is enough? TIA

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      It's a good idea if the object can be accessed afterwards because then the code can test whether FileStreamObj is null before trying to access it and getting ObjectDisposedException which will consume more resources.  If nothing else can access it after it's disposed, e.g. it's used as a variable in a function of some sorts then there probably isn't any need.


      As of how to accomplish this I wouldn't have a clue at the moment and I'm too lazy to google it

      1 Reply Last reply
      0
      • X xkx32

        Hi guys I'm wondering if I should set the objects that I create to null after I'm done with it... let's say I create FileStream object in private method, after I close the object should I write: FileStreamObj.close(); FileStreamObj = null; or: FileStreamObj.close(); Is enough? TIA

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        Is FileStreamObj going out of scope after you close it? If so, then you don't have to null the handle.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        1 Reply Last reply
        0
        • X xkx32

          Hi guys I'm wondering if I should set the objects that I create to null after I'm done with it... let's say I create FileStream object in private method, after I close the object should I write: FileStreamObj.close(); FileStreamObj = null; or: FileStreamObj.close(); Is enough? TIA

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Closing the file is enough. Only set it to null if the reference is going to hang around and some code may try to use it. By setting it to null in that scenario you have a nice easy test to see if it is valid e.g. if (FileStreamObj == null)


          Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

          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