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