eTreamReader何时在C#中变为null?

发布于 2025-01-21 12:28:40 字数 588 浏览 2 评论 0 原文

我看到了此链接:

代码的一部分是:

if (!File.Exists(filename))
    throw new FileNotFoundException("The file does not exist.");

this.filename = filename;
string txt = String.Empty;
StreamReader sr = null;
try
{
    sr = new StreamReader(filename);
    txt = sr.ReadToEnd();
}
finally
{
    if (sr != null) sr.Dispose();
}

eTreamReader何时null?

I saw this link:

https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-6.0

The part of the code is:

if (!File.Exists(filename))
    throw new FileNotFoundException("The file does not exist.");

this.filename = filename;
string txt = String.Empty;
StreamReader sr = null;
try
{
    sr = new StreamReader(filename);
    txt = sr.ReadToEnd();
}
finally
{
    if (sr != null) sr.Dispose();
}

When does StreamReader become null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2025-01-28 12:28:40

在代码中,首先检查文件是否存在,然后创建 StreamReader 读取该文件。通过询问 streamReader 何时null,您询问它何时可以构造异常。它实际上可以在几种情况下发生。

如果:

  • 该过程没有在文件上读取特权,或者其他人当前正在使用该文件。毕竟,只检查了文件是否存在,但是上帝知道操作系统是否真的允许该过程读取文件。
  • 该文件在 streamWriter 构造期间不存在。现在我知道您在想什么:“但是它只是检查了文件是否存在”。是的,但是请考虑以下情况:
  1. 检查文件是否存在
  2. (可选),碰巧的是,在检查后,OS线程调度程序决定暂停某些毫秒的运行线程,以腾出其他(可能更高的优先级)线程空间
  3. 调用了其他一些线程,另一个过程或用户删除文件
  4. streamReader 构造器,并且该文件不再存在,

摘要不再存在,任何事先检查的数量都无法100%保证您将能够在下一个纳秒中读取文件。

In the code, it is first checked if the file exists, then a StreamReader is created which reads that file. By asking when can the StreamReader be null, you are asking when can it's constructor throw an exception. It can actually happen in several situations.

The constructor will throw an exception if:

  • The process doesn't have read priviledges on the file OR someone else is currently using the file. After all, it was only checked that the file exists, but god knows if the OS will actually allow the process to read the file.
  • The file doesn't exist during StreamWriter construction. Now I know what you're thinking: "But it just checked that the file exists". Yes, but consider this scenario:
  1. it is checked that the file exists
  2. (optional) it just so happens that after the check, the OS thread scheduler decides to suspend the running thread for some milliseconds to make room for other (possibly higher priority) threads
  3. Some other thread, another process, or the user deletes the file
  4. StreamReader constructor is called, and the file doesn't exist anymore

In summary, no amount of prior checking can 100% guarantee you will be able to read the file in the next nanosecond.

安穩 2025-01-28 12:28:40

我认为您的问题是关于 null 中检查部分。

streamReader sr 变量可以在最后 part中的 null ,如果在 new StreamReader中执行构造函数期间提出异常(文件名)

I think your question is regarding the null check in the finally part.

The StreamReader sr variable may be null in the finally part, if an exception is raised during the execution of the constructor in new StreamReader(filename).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文