Question

Coming from C/C++, I am used to use pointers to handle most of my operations however I understand the importance of using "using" statement when streamreader/streamwriter is used.

However I am facing a peculiar issue in the sense that I have one method/function that opens/declares the streamreader/streamwriter and I have another method/function to actually read/write to the stream. But the "using" statement is only local so there is no way for me to properly use the "using" statement in the function that does the actual reading/writing. The streamreader/streamwrite are defined in the class definition that is accessible to the class itself only. the two methods are both class functions.

This might be a very newbie question but I couldnt find anything useful online. Thank you.

Was it helpful?

Solution

Well, if you need to return IDisposable object outside of the scope where it is created, you can do so, provided that some other code somewhere calls .Dispose() on it once it is no longer used.

However, for a more complete answer, you should post some code. A refactor may be possible to help avoid the issue altogether.

OTHER TIPS

using() { ... } is just a convenient way of calling .Dispose() at the end of your scope.
You don't always have to use using() { ... }. You might want to handle the lifetime of your object explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top