Consider the following:
TextReader reader = new StreamReader(file);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(reader);
And
using (TextReader reader = new StreamReader(file))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(reader);
}
What will actually happen in the latter piece of code? Will the Dispose() be called?
Yes it will be called.
The using
statement is syntactic sugar for:
try
{
// Do stuff
return;
}
finally
{
// Dispose
}
and the finally
will get called even on the return
.
So you are safe to use this.
Yes, the Dispose
will be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With