using 语句的用法

发布于 2024-08-22 22:32:04 字数 593 浏览 6 评论 0原文

可能的重复:
哪个更好,何时: 在 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 技术交流群。

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

发布评论

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

评论(5

爱,才寂寞 2024-08-29 22:32:05

using 语句适用于任何实现 IDisposable 的对象。

using (var my_object = new IDisposableObject ())
{

    //do my_object code here.


} //once the program reaches here, it calls my_object.Dispose();

通常,这用于具有连接的对象,当程序完成它们时需要手动处理(关闭)它们。例如,打开与文件和数据库的连接。

即使代码中存在错误,using 语句也会调用 Dispose,因此它类似于在 try catch 语句的 finally 块中调用 Dispose 方法。

示例/教程

The using statement is for any object which implements IDisposable.

using (var my_object = new IDisposableObject ())
{

    //do my_object code here.


} //once the program reaches here, it calls my_object.Dispose();

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

め七分饶幸 2024-08-29 22:32:05

这是一个更短的语法,以确保调用 dispose:

using (File f = File.Open("..."))
{

}

File f;
try
{
  f = File.Open("...");
}
finally
{
  f.Dispose();
}

It's a shorter syntax to make sure that dispose is called:

using (File f = File.Open("..."))
{

}

is the same as

File f;
try
{
  f = File.Open("...");
}
finally
{
  f.Dispose();
}
記憶穿過時間隧道 2024-08-29 22:32:05

使用 using 语句有两种基本方法。摘自使用指令 (C#) MSDN。

  • 为命名空间创建别名(using 别名)。
  • 允许在命名空间中使用类型,这样您就不必限定该命名空间中类型的使用(using 指令)。

There are 2 fundamental ways you can use the using statement. As extracted from using Directive (C#) from MSDN.

  • Create an alias for a namespace (a using alias).
  • Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).
何时共饮酒 2024-08-29 22:32:05

只是为了扩展 Kevin 的答案,using 语句有效地将对象实例化包装在 try/finally 块中,调用finally 部分中的对象 Dispose() 方法,即与

using(myObject m = new myObjecyt())
{
   // Code here
}

相同。

myObject m = new myObjecyt()
try
{
   // Code here
}
finally
{
   m.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.

using(myObject m = new myObjecyt())
{
   // Code here
}

is the same as

myObject m = new myObjecyt()
try
{
   // Code here
}
finally
{
   m.Dispose();
}

this can be verified by checking the MSIL.

櫻之舞 2024-08-29 22:32:05

“Using”关键字有助于在 .net 中安全、清晰地完成某件事。这是正确地处置某些对象。您可能已经了解了 .Net 中如何进行垃圾回收,这意味着对于许多对象,当我们使用完它们后,我们不必关心它们。然而,其他对象需要调用一个名为 Dispose 的方法。最佳实践是,每当一个对象具有 Dispose 方法时,我们就应该在处理完该对象后调用该方法。

(他们通常处理非托管资源 这意味着它正在使用不受 .NET 运行时控制的内存或其他计算机部分,因此,当垃圾收集到达丢弃的 .Net 对象时,它无法正确释放这些资源,这可能会导致内存不足。一个很好的例子是 ADO.NET Connection 对象。反复不释放连接对象可能会导致数据库问题。)

Using 关键字也与此“Dispose”相关。方法。更准确的说法是,当一个对象具有 Dispose 方法时,我们要么 (A.) 在完成后调用 .Dispose,要么 (B.)将使用该对象的代码放在 Using 块 中。使用块为您做了几件事:

  • 当代码移出该块时,会自动为您调用重要的 Dispose 方法。
  • 更重要的是,如果代码块中出现错误,Dispose方法仍然会被调用。这就是为什么 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:

  • When the code moves out of the block, the important Dispose method is automatically called for you.
  • More importantly, if there is an error in the code block, the Dispose method will still be called. This is why the using block is really helpful. Otherwise you have to put in a lot of error handling code.

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.

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