C#“使用”块
我有类似下面的代码...这里有人提到 WebClient、Stream 和 StreamReader 对象都可以从使用块中受益。两个简单的问题:
1:这个小片段在使用块时会是什么样子?我自己做研究没有问题,所以资源链接很好,但只看一个例子会更快更容易,我会从中理解它。
2:我想养成良好的编码标准的习惯,如果我了解一点使用块更好的原因会有所帮助......这只是为了让你不必担心关闭还是在那里还有更多理由吗?谢谢!
WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);
JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];
stream.Close()
reader.Close()
I've got something like the code below...someone here mentioned that the WebClient, Stream, and StreamReader objects could all benefit from using blocks. Two easy questions:
1: How would this little snippet look with using blocks? I've no problem with doing my own research so resource links are fine but it'd be quicker and easier to just see an example and I'll understand it from that.
2: I'd like to get in the habit of good coding standards, would help if I knew a little about the reasons why using blocks are better...is it just so you don't have to worry about closing or are there more reasons? Thanks!
WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);
JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];
stream.Close()
reader.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
来自 MSDN:using 语句(C# 参考)
From MSDN: using Statement (C# Reference)
或者简单地:
or simply:
与使用相同
更容易使用
is the same as
A lot easier to use using
它很简单:
使用
*using*
本身并不是“良好实践”,而是一种处理您应该处理的对象的简短方法(语法糖)。诸如文件、数据库连接以及网络之类的东西。您可以这样做:
这基本上只是正常使用变量
we
的快捷方式,然后调用we.Dispose()
进行清理。语法:
您应该看到的其他 SO 问题:
using 关键字和 IDisposable 接口之间有什么关系?
在 using 语句中使用各种类型 (C#)
Its simple :
Using
*using*
is not "good practice" as such, more a short way (syntactic sugar) of disposing objects that you should be disposing. Things like Files, Connections to a Database and in your case a network.You would do something like:
This is basically just a shortcut for using the variable
we
normally and then callingwe.Dispose()
which does a clean up.Syntax :
Other SO questions you should see:
What is the relationship between the using keyword and the IDisposable interface?
using various types in a using statement (C#)
像这样的事情:
至于为什么
using
块很好,并且比手动调用 Dispose... 更好,如果该using
块中的任何代码在您之前抛出异常到达关闭所有内容的那一行?您实际上会泄漏 IDisposable 对象在幕后管理的任何非托管资源。using
确保 Dispose 被正确调用,即使面对异常(本质上是通过注入适当的 try/finally 块)。如果可能(即,您不必跨范围保留某些 IDisposable 的生命周期),您应该利用
using
块(如果没有其他原因,只是它们减少了您必须编写的样板代码量)以确保您自己的代码安全正确。Something like this:
As for why
using
blocks are good, and better than manually calling Dispose... image if any of the code in thatusing
block threw an exception before you hit the lines where you close everything? You'd essentially leak whatever unmanaged resource the IDisposable object is managing under the hood.using
ensures that Dispose is called correctly, even in the face of an exception (by essentially injecting the appropriate try/finally block).When possible (i.e., you don't have to preserve the lifetime of some IDisposable across scopes), you should leverage
using
blocks if for no other reason than they reduce the amount of boilerplate code you have to write in order to ensure your own code is safe and correct.使用 {} 块只需在右大括号处调用 Dispose(),或者更确切地说,告诉垃圾收集器它可以处置该对象。你会这样使用它:
using {} blocks simply call Dispose() at the closing brace--or rather, tell the Garbage Collector that it can dispose of the object. You'd use it like:
@Darin 的答案显示了代码。它们好的原因是,使用块会导致编译器吐出代码,在退出块之前自动对对象调用“Dispose”(使其立即释放可能正在使用的任何资源) - 即使抛出异常在块内
@Darin's answer shows the code. The reason they are good is that using blocks cause the compiler to spit out code that will automatically call "Dispose" on the object (to have it immediately release any resources it may be using) before exiting the block - even if an exception gets thrown within with the block
using
相当于try..finally
,因此即使块内抛出异常,处理程序也会运行。using
is equivalent totry.. finally
so the disposer will run even if an exception is thrown within the block.使用块有两个原因:
最后, using-block 例如
与以下构造相同:
There are 2 reasons for using-blocks:
In the end a using-block e.g.
is identical to the followin contstruct: