I have the following logic:
try
{
using (var contents = new StreamReader(file.InputStream).ReadToEnd())
{
var rows = contents.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
rows.ForEach(r => mids.Add(r.Split(',')[0]));
}
}
catch(IOException e)
{}
finally
{
contents = null;
}
In the using statement I have an error in the question. It happened probably because I use .ReadToEnd() method.
Without the using statement I would need to use try/catch/finally for a clean up (to fix veracode resource clean up issue)
How can I fix that, so I don't need to use try\catch\finally and use only the using statement?
So,
usingshould be used with object which implementsIDisposableinterface. You callingReadToEndmethod which returnsstringandcontentsis not aIDisposable(because string is not). You should use it like this:You want to clean up
StreamReader,contentswill be collected by GC when method will finished because it has typestring.