快速跳转到Reflector.NET可以看到打开的
Close()方法
StreamWriter是:
public override void Close(){ this.Dispose(true); GC.SuppressFinalize(this);}并且
StreamReader是:
public override void Close(){ this.Dispose(true);}将
Dispose(bool disposing)在覆盖
StreamReader为:
protected override void Dispose(bool disposing){ try { if ((this.Closable && disposing) && (this.stream != null)) { this.stream.Close(); } } finally { if (this.Closable && (this.stream != null)) { this.stream = null; base.Dispose(disposing); } }}的
StreamWriter方法是相似的。
因此,阅读代码后,很明显,您可以随意随意地调用
Close()&
Dispose()在流上进行调用。它不会以任何方式改变行为。
所以它归结为它是否是更具可读性的使用
Dispose(),
Close()和/或
using ( ... ) { ... }。我个人的喜好是,
using ( ... ) { ... }应尽可能始终使用它,因为它可以帮助您“不用剪刀剪”。但是,尽管这有助于正确性,但确实会降低可读性。在C#中,我们已经有过多的右花括号,因此我们如何知道哪个实际上对流执行了大括号?
所以我认为最好这样做:
using (var stream = ...){ stream.Close();}它不会影响代码的行为,但确实有助于提高可读性。



