I would like to create a method that returns an XmlReader. Depending on the circumstances, the XmlReader can be powered by various types of streams, either StringReader or MemoryStream.
Usually I delete a StringReader or a MemoryStream using a block, but since I want to return an XmlReader instead, I cannot do this if I want to go with this design. I do not expect MemoryStream to allocate huge amounts of memory, so I can live with a slight delay in freeing up resources.
Are there any consequences that the GC may use StringReader and MemoryStream in this case?
I must clarify that this is a practical question, not a question with best practice. Obviously, the theory dictates that I have to clean my own resources, but the theory also says that I should prefer the simplest design possible for maximum maintainability. In some cases, a violation of best practice may be justified IMHO, and my question is whether this particular case justifies a violation of best practice.
Also, we are talking only about StringReader and MemoryStream, and not about a common stream or reader. My reason for justifying it in this case is that the actual creation of StringReader / MemoryStream is well encapsulated in the method that XmlReader returns, so it is controlled that XmlReader will not load the stream with a limited resource.
source share