Click here to Skip to main content
12,399,471 members (52,525 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# 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
Updated 15-Sep-12 2:46am
v2
Comments
Wes Aday 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2016
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