This is my code:
string data = "...";
var stream = new StreamReader(data);
Is that okay not call the Close()
method?
This is my code:
string data = "...";
var stream = new StreamReader(data);
Is that okay not call the Close()
method?
Yes, otherwise you will have a memory leak. Wrap your StreamReader
in a using statement so you don't have to worry about cleaning it up e.g.
using (var reader = StreamReader(data))
{
...
}
Have you actually compiled and run your code?
The StreamReader(string)
constructor treats the input as a file name!
Unless this isn't really your code and you meant StringReader
, your code is trying to stream the contents of the file name specified in data
, which is likely to throw a FileNotFoundException
because the file probably doesn't exist. And if it did, you would certainly need to call Close
or integrate your code into a using
statement to release the file handle.
You might also want to take a look at this tutorial on msdn:
http://msdn.microsoft.com/en-us/library/aa355056.aspx
which tells you about things you need to be careful with the using
statement. Other than that, using is the way to go.
Another pretty good article on codeProject. Worth reading.
The resource will not be accessable to other processes until your process stops using it so you should close it if you don't need it