0

Is it possible to create an extension method similar to this? How should I handle Closing() the stream when finished?

    public static TextReader ToTextReader(this string XML)
    {
        StringReader sr = new StringReader(XML);
        return sr;
    }
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

2 Answers2

3

You don't need to; when the calling code closes the TextReader (as it should), the Stream is automatically closed as well. There's no need to close both objects individually.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
2

You shouldn't. It's the responsibility of the caller to dipsose of the TextReader object and the corresponding Stream

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454