Just for an example of creating a MD5 hash, microsoft provided this code sample:
MSDN Code
// in Helpers class
public static string GetMd5Hash(MD5 md5Hash, string input)
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
and using it would be done like this
public string CreateHash(string str)
{
using (var md5Hash = MD5.Create())
{
return Helpers.GetMd5Hash(md5Hash, str);
}
}
My Code
I thought changing the code Microsoft provided to this, would make it easier regarding reusability of the helper method:
// in Helpers class
public static string GetMd5Hash(string input)
{
using (var md5Hash = MD5.Create())
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
So reusing it creates less code lines as a result:
public string CreateHash(string str)
{
return Helpers.GetMd5Hash(str);
}
So what is the purpose of not doing it like I did in my second example? My initial thought was, that programmers, who use the code MSDN provided, would always be aware of the fact, that they use unmanaged resources (is this the right term?), whereas they wouldn't if they used my implementation.
Any other thoughts?