Click here to Skip to main content
11,720,785 members (78,832 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 cryptography
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();
}
Example 2:
using (SHA1 hash = SHA1.Create())
{
    txtSHA1.Text = GetBytesToString(hash.ComputeHash(inputData));
}
Posted 15-Sep-12 2:28am
Edited 15-Sep-12 2:46am
v2
Comments
Wes Aday at 15-Sep-12 9:07am
   
2 is correct. 1 is totally unnecessary and redundant.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100