Can I return a StreamReader from a method?
Can I return a StreamReader from开发者_运维知识库 a method ?
Yes, of course. It's not a great idea, though - you're creating the StreamReader in one method, and closing it in another. It's better practice to create and close the reader in one method.
Sure. Using normal IDispose semantics, here is how it would look:
StreamReader MakeStreamReader () {
return new StreamReader ("somefile.txt");
}
void Caller () {
using (StreamReader r = MakeStreamReader ())
Console.WriteLine (r.ReadToEnd ());
}
精彩评论