最后与使用 [英] Finally vs Using

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

问题描述

最终之间有什么区别?和使用阻止?


是否仍然需要(或建议)使用尝试和捕获

a中的区块使用块?


-

问候,


Fred Chateau

e- mail:fchateauAtHotelMotelNowDotCom

解决方案

我相信他们彼此没有关系。


finally用于确保代码被执行,无论是否在try / catch块中发生了

异常。 using语句只是一个

的快捷方式,用于初始化依赖于Dispose方法的变量。

当使用时块完成后,系统会自动调用对象上的

Dispose方法。


你对每个构造的使用与另一个构造的使用无关。


Fred Chateau < FC ****** @ 127.0.0.1>在消息中写道

新闻:uZ ************** @ TK2MSFTNGP12.phx.gbl ...

有什么区别a终于和使用阻止?

是否仍然需要(或建议)使用尝试和捕获在使用中阻止
挡?

-
问候,

Fred Chateau
电子邮件:fchateauAtHotelMotelNowDotCom





你可以测试一个这样的例子:

当你使用时,它可能会向客户端抛出异常的块,它可能没有
处理它并释放相关资源。但是try / catch / finally将处理

并释放revant资源成功。它自己可以理解它。


-

---------?×?oé(????ó¥)------- -----

MSN:xueyunying @ hotmailcom
http: //shanquan.blogone.net

邮箱:ta *** @ gentle.com.cn

·??í?aê?£?12' '?¤?é£?12í?3é?í

分享知识,分享愿景,分享

成功

Fred Chateau < FC ****** @ 127.0.0.1> D'è?óê?t

新闻:uZ ************** @ TK2MSFTNGP12.phx.gbl ...

什么是最终之间的差异和使用阻止?

是否仍然需要(或建议)使用尝试和捕获在使用中阻止
挡?

-
问候,

Fred Chateau
电子邮件:fchateauAtHotelMotelNowDotCom



using(MyResource res = new MyResource())

{

DoSomething(res);

}


如果严格相当于:


{

MyResource res = new MyResource();

尝试

{

DoSomething(res);

}

终于

{

res.Dispose();

}

}


所以,我的建议是使用第一种形式而不是第二种形式。如果你这么做,那么最终应该没有什么用处,因为你需要一个finally块的大多数情况

应该与资源处理有关并且

使用使用编码而不是最终。


Bruno。


Fred Chateau < FC ****** @ 127.0.0.1>一个écritdansle message de

新闻:uZ ************** @ TK2MSFTNGP12.phx.gbl ...

什么是最终之间的差异和使用阻止?

是否仍然需要(或建议)使用尝试和捕获在使用中阻止
挡?

-
问候,

Fred Chateau
电子邮件:fchateauAtHotelMotelNowDotCom



What are the differences between a "finally" and a "using" block?

Is it still necessary (or advisable) to use "try" and "catch" blocks within
a "using" block?

--
Regards,

Fred Chateau
e-mail: fchateauAtHotelMotelNowDotCom

解决方案

I believe they have no relation to each other.

"finally" is used to ensure that code gets executed regardless of whether an
exception occurred or not in a try/catch block. A using statement is just a
shortcut for way of initializing variables that rely on the Dispose method.
When the "using" block finishes, the system will automatically call the
Dispose method on the object.

Your use of each construct is independent of the use of the other one.

"Fred Chateau" <fc******@127.0.0.1> wrote in message
news:uZ**************@TK2MSFTNGP12.phx.gbl...

What are the differences between a "finally" and a "using" block?

Is it still necessary (or advisable) to use "try" and "catch" blocks within a "using" block?

--
Regards,

Fred Chateau
e-mail: fchateauAtHotelMotelNowDotCom



Hi,
you can test a example like this:
the block it may throw a exception to client,when you use using,it may not
handle it and release related resources.but try/catch/finally will handle
and release revant resource success.try it yourself may can understand it .

--
---------ì?×?oé(????ó¥)------------
MSN:xueyunying@hotmailcom
http://shanquan.blogone.net
Mail:ta***@gentle.com.cn
·??í?aê?£?12′′?¤?é£?12í?3é?í
Share Knowledge,Share Vision,Share
Success
"Fred Chateau" <fc******@127.0.0.1> D′è?óê?t
news:uZ**************@TK2MSFTNGP12.phx.gbl...

What are the differences between a "finally" and a "using" block?

Is it still necessary (or advisable) to use "try" and "catch" blocks within a "using" block?

--
Regards,

Fred Chateau
e-mail: fchateauAtHotelMotelNowDotCom



using (MyResource res = new MyResource())
{
DoSomething(res);
}

if strictly equivalent to:

{
MyResource res = new MyResource();
try
{
DoSomething(res);
}
finally
{
res.Dispose();
}
}

So, my advice is to use the first form rather than the second one. If you do
this, there should be little use for finally, because most of the situations
where you need a finally block should be related to resource disposal and
coded with "using" rather than finally.

Bruno.

"Fred Chateau" <fc******@127.0.0.1> a écrit dans le message de
news:uZ**************@TK2MSFTNGP12.phx.gbl...

What are the differences between a "finally" and a "using" block?

Is it still necessary (or advisable) to use "try" and "catch" blocks within a "using" block?

--
Regards,

Fred Chateau
e-mail: fchateauAtHotelMotelNowDotCom



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

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