Click here to Skip to main content
12,952,159 members (55,454 online)
Rate this:
 
Please Sign up or sign in to vote.
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
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2017
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