gpt4 book ai didi

c# - 在 streamwriter 、 streamreader 之前使用 using 语句的好处/使用

转载 作者:行者123 更新时间:2023-11-30 19:18:00 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What is the C# Using block and why should I use it?

所以我刚刚注意到,在 msdn 示例和一些 stackoverflow 问题中,在 streamwriter 等之前使用 using 语句的地方有答案,但实际上有什么好处?因为我从来没有被教导/告知/阅读任何使用它的理由。

            using (StreamReader sr = new StreamReader(path)) 
{
while (sr.Peek() >= 0)
Console.WriteLine(sr.ReadLine());
}

代替:

            StreamReader sr = new StreamReader(path);
while (sr.Peek() >= 0)
Console.WriteLine(sr.ReadLine());

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com