开发者

Extension Method from String to TextReader... how do I close the stream?

开发者 https://www.devze.com 2023-02-06 05:05 出处:网络
Is it possible t开发者_开发技巧o create an extension method similar to this?How should I handle Closing() the stream when finished?

Is it possible t开发者_开发技巧o 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;
    }


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消