如果IDisposposable,请正确使用 [英] Appropriate use if IDisposable

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

问题描述

是否需要在下面的代码块中使用IDisposable.我正在使用,并且散列包含从用户获取的文本的SHA1,而inputData是字节数组.我认为第二段代码是正确的.

示例1:

Is there any need to use IDisposable in the block of code below. I am availing of using and hash contains the SHA1 of text taken in from the user and inputData is a byte array. I assume the second block of code is correct.

Example 1:

using (SHA1 hash = SHA1.Create())
{
    txtSHA1.Text = GetBytesToString(hash.ComputeHash(inputData));
    ((IDisposable)hash).Dispose();
}


示例2:


Example 2:

using (SHA1 hash = SHA1.Create())
{
    txtSHA1.Text = GetBytesToString(hash.ComputeHash(inputData));
}

推荐答案

使用第二个选项,如果您能够使用"using"语句意味着该对象是从IDisposable类继承的,则他们已经编写了适当的处理方法事件.
Go with second option, if you are able to use "using" statement means, that object is inherited from IDisposable class, they have written appropriate dispose events.


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

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