匿名的StreamReader会自动关闭吗?

huangapple go评论85阅读模式
英文:

Will an anonymous StreamReader close automatically?

问题

以下是翻译好的部分:

首先,使用FileStream的好处是什么?我明白StreamReader可以直接接受文件路径。

using (var reader = new StreamReader(CustomSettingsFile.FullName))
{
    var data = reader.ReadToEnd();
}

其次,如果我只使用一次读取器,难道我不能只使用匿名方式并省略using吗?

var data = new StreamReader(CustomSettingsFile.FullName).ReadToEnd();
英文:

I've found the following code in a project I'm working on and I'm curious to know if it can be written more succinctly.

using (var stream = new FileStream(CustomSettingsFile.FullName, FileMode.Open, FileAccess.Read))
{
    using (var reader = new StreamReader(stream))
    {
        var data = reader.ReadToEnd();
    }
}

Firstly, what's the benefit of using FileStream? I understand that StreamReader can take a file path directly.

using (var reader = new StreamReader(CustomSettingsFile.FullName))
{
    var data = reader.ReadToEnd();
}

Secondly, if I'm only using the reader once, can't I just anonymize it and omit the using?

var data = new StreamReader(CustomSettingsFile.FullName).ReadToEnd();

答案1

得分: 3

I'm curious to know if it can be written more succinctly.
首先,我想知道是否可以更加简洁地编写它。

First thing you can do in this specific case is to omit extra curly braces:
在这种特定情况下,你可以首先省略额外的花括号:

using (var stream = new FileStream(CustomSettingsFile.FullName, FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
    var data = reader.ReadToEnd();
}

Firstly, what's the benefit of using FileStream? I understand that StreamReader can take a file path directly.
首先,使用FileStream有什么好处?我理解StreamReader可以直接接受文件路径。

Based on the current implementation none, except for explicitly specifying the FileMode and FileAccess, though as far as I can see StreamReader will use the same defaults.

根据当前的实现,除了明确指定FileModeFileAccess之外,没有好处,尽管据我所见,StreamReader将使用相同的默认值。

Secondly, if I'm only using the reader once, can't I just anonymize it and omit the using?
其次,如果我只使用一次读取器,我不能只将其匿名化并省略使用吗?

No. You need to deterministically free the unmanaged resources when you have finished using them. General rule of thumb: If something implements IDisposable then it should be disposed (for example via using which handles exceptions).

不可以。在使用完它们后,你需要确定性地释放非托管资源。一般的经验法则是:如果某物实现了IDisposable,那么它应该被释放(例如通过using来处理异常)。

Note that since C# 8.0 you can leverage using declarations in some cases, which will call dispose at the end of the current scope:

请注意,自从C# 8.0以来,在某些情况下,你可以利用using declarations,它将在当前作用域的末尾调用dispose:

using var reader = new StreamReader(CustomSettingsFile.FullName);
var data = reader.ReadToEnd();

Also, in case when you don't actually need streaming (i.e., you need the whole data at a time), consider using File.ReadAllText which will do all the work for you.

此外,在实际上不需要流式传输(即,你需要一次性获取整个数据)的情况下,考虑使用 File.ReadAllText,它会为你完成所有工作。

Read also:
还阅读:

英文:

> I'm curious to know if it can be written more succinctly.

First thing you can do in this specific case is to omit extra curly braces:

using (var stream = new FileStream(CustomSettingsFile.FullName, FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
    var data = reader.ReadToEnd();
}

> Firstly, what's the benefit of using FileStream? I understand that StreamReader can take a file path directly.

Based on the current implementation none, except for explicitly specifying the FileMode and FileAccess, though as far as I can see StreamReader will use the same defaults.

> Secondly, if I'm only using the reader once, can't I just anonymize it and omit the using?

No. You need to deterministically free the unmanaged resources when you have finished using them. General rule of thumb: If something implements IDisposable then it should be disposed (for example via using which handles exceptions).

Note that since C# 8.0 you can leverage using declarations in some cases, which will call dispose at the end of current scope:

using var reader = new StreamReader(CustomSettingsFile.FullName);
var data = reader.ReadToEnd();

Also in case when you don't actually need streaming (i.e. you need the whole data at a time) consider using File.ReadAllText which will do all the work for you.

Read also:

答案2

得分: 1

我很好奇是否可以更简洁地编写。

您可以使用File.ReadAllTextFile.ReadAllTextAsync方法来代替。

它们在底层创建并处理了StreamReader 内部

英文:

> I'm curious to know if it can be written more succinctly.

You can use File.ReadAllText or File.ReadAllTextAsync methods instead.

They create and dispose StreamReader under the hood.

huangapple
  • 本文由 发表于 2023年8月10日 17:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定