Often is it necessary to have a system with an optimized memory management. If you use local objects, for example StringReader, the created object must be disposed at the end of the method. You have two ways to do this:
- Dispose() the StringReader object
- Use the using statement as follows:
XmlSerializer ser = new XmlSerializer(classType);
using (StringReader reader = new StringReader(xmlString))
Here after the using statement, the StringReader object will be destroyed after the curly brackets. The advantage of this method to the use of Dispose() is that if a exception occurs the StringReader object will be destroyed anyway. When just using Dispose() it is not guaranteed that the object will be disposed.
For more information: MSDN Link