using语句的使用 [英] Usage of a using statement

查看:140
本文介绍了using语句的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:结果
哪一个更好,而当:?using语句或调用上了IDisposable的Dispose()在C#中结果
什么时候应该使用“使用”的使用使用if语句在C#块?结果
<? / A>

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?

正确,我将如何使用使用语句?我有一个教程开放的,我不明白。我可以看到实现超过1不同的方式。这是正确的还是借道?

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?

推荐答案

using语句是它实现IDisposable的任何物体。

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.

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

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.

示例/教程

这篇关于using语句的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆