using 语句的用法
可能的重复:
哪个更好,何时: 在 C# 中使用语句或对 IDisposable 调用 Dispose() ?
什么时候应该在 C# 中使用“using”块?< br> 使用 using if 语句?
正确地说,我将如何使用 using< /代码>声明?我打开了一个教程,但我不明白它。我可以看到不止 1 种不同的实施方式。哪个是正确的或受欢迎的方式?
Possible Duplicates:
Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?
When should I use “using” blocks in C#?
using a using if statement?
Properly, how will I use a using
statement? I have a tutorial open and i do not understand it. And i can see more than 1 different ways to implement. Which is correct or favored way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
using 语句适用于任何实现 IDisposable 的对象。
通常,这用于具有连接的对象,当程序完成它们时需要手动处理(关闭)它们。例如,打开与文件和数据库的连接。
即使代码中存在错误,using 语句也会调用 Dispose,因此它类似于在 try catch 语句的 finally 块中调用 Dispose 方法。
示例/教程
The using statement is for any object which implements IDisposable.
Generally, this is used for objects with connections that manually need to be handled (closed) when the program is finished with them. For example, open connections to files and to the database.
The using statement will call Dispose even if there is an error in the code so it is akin to calling the Dispose method in a finally block of the try catch statement.
Example/Tutorial
这是一个更短的语法,以确保调用 dispose:
与
It's a shorter syntax to make sure that dispose is called:
is the same as
使用
using
语句有两种基本方法。摘自使用指令 (C#) MSDN。There are 2 fundamental ways you can use the
using
statement. As extracted from using Directive (C#) from MSDN.只是为了扩展 Kevin 的答案,using 语句有效地将对象实例化包装在 try/finally 块中,调用finally 部分中的对象 Dispose() 方法,即与
相同。
可以通过检查 MSIL 来验证的
Just to expand on Kevin's answer, a using statement effectively wraps your object instantiation in a try/finally block calling the object Dispose() method in the finally section i.e.
is the same as
this can be verified by checking the MSIL.
“Using”关键字有助于在 .net 中安全、清晰地完成某件事。这是正确地处置某些对象。您可能已经了解了 .Net 中如何进行垃圾回收,这意味着对于许多对象,当我们使用完它们后,我们不必关心它们。然而,其他对象需要调用一个名为 Dispose 的方法。最佳实践是,每当一个对象具有 Dispose 方法时,我们就应该在处理完该对象后调用该方法。
(他们通常处理非托管资源 这意味着它正在使用不受 .NET 运行时控制的内存或其他计算机部分,因此,当垃圾收集到达丢弃的 .Net 对象时,它无法正确释放这些资源,这可能会导致内存不足。一个很好的例子是 ADO.NET Connection 对象。反复不释放连接对象可能会导致数据库问题。)
Using 关键字也与此“Dispose”相关。方法。更准确的说法是,当一个对象具有 Dispose 方法时,我们要么 (A.) 在完成后调用 .Dispose,要么 (B.)将使用该对象的代码放在 Using 块 中。使用块为您做了几件事:
关键是,对于许多具有 Dispose 方法的对象来说,错误处理尤其重要。对于我们的很多代码来说,我们不需要错误处理;错误发生的后果并不是真正的问题。但对于这些 IDisposable 对象来说,错误常常是一个问题,甚至可能是一个大问题。因此,.Net 为忙碌的开发人员提供了一种语法来添加最基本的错误处理并继续前进。始终至少从一个Using块开始;也许稍后您会转向更高级的错误处理,但至少您已经获得了基本的安全性。
这是对Using关键字的详细解释。
The "Using" keyword helps do a certain thing to be done safely and clearly in .net. This is propertly disposing of certain objects. You may have learned how in .Net we have garbage collection, which means for many object we don't have to care about them when we are done using them. Other object however need to have a method called on them called Dispose. The best practice is that whenever an object has a Dispose method, then we should call that method when we're done with that object.
(They typically handle unmanaged resources. This means that it's using memory or other computer parts that are outside the control of the .NET runtime. So, when garbage collection reaches the discarded .Net object, it is unable to propertly let go of these resources. This can cause memory leaks and all kinds of other problems. A good example is an ADO.NET Connection object. Repeatedly not Disposing of your connection objects can cause DB problems.)
The Using keyword is also tied to this "Dispose" method. It's more accurate to say that when an object has a Dispose method, we either (A.) call .Dispose when we're done with it, or (B.) put our code that uses that object within a Using block. The Using block does several things for you:
The key is that for many of these object that have a Dispose method, error handling is particularly imortant. For a lot our code we don't need error handling; the consequences of an error happening are not really a problem. But for these IDisposable objects errors are often a problem, maybe a big problem. So, .Net provides a syntax for busy developers to add the most basic error handling and move on. Always start off with a Using block at least; maybe later you'll move to fancier error handling, but at least you've got this basic safety.
Here's a good explanation of the Using keyword.