If I have this method:
public StreamReader GetResourceStream(string filename)
{
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(filename))
using (StreamReader sr = new StreamReader(stream))
{
return sr;
}
}
When I call it, should I call it this way
StreamReader resource = GetResourceStream("blah.xml");
or this way:
using (StreamReader resource = GetResourceStream("blah.xml"))
{
// Do stuff
}
If second way, does this means the using (StreamReader sr = new StreamReader(stream)) line is not making any difference by using a using statement?
It doesn’t really make sense to have the
GetResourceStreammethod put the using statement around the object it’s returning. That means you will be returning an object in a disposed state, and what good is that? It will throwObjectDisposedexceptions at you.So remove the
usingstatements inside your factory method, and apply theusingstatement where you’re actually using it.It should look more like this: