加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么我们在c#中使用streamreader前使用

发布时间:2020-12-15 07:44:29 所属栏目:百科 来源:网络整理
导读:为什么我们在c#中使用streamreader前使用 using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Cons
为什么我们在c#中使用streamreader前使用
using (StreamReader sr = new StreamReader("TestFile.txt")) 
{
    string line;
    // Read and display lines from the file until the end of 
    // the file is reached.
    while ((line = sr.ReadLine()) != null) 
    {
        Console.WriteLine(line);
    }
}

解决方法

在处理一次性物体时,在C#中使用块非常方便.一次性对象是那些可以在调用dispose时显式释放它们使用的资源的对象.我们知道.Net垃圾收集是非确定性的,因此您无法预测对象何时会被垃圾收集.

阅读这篇文章了解更多详情:understanding ‘using’ block in C#

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读