For example this encryption function could be called a Utility ?
public static string HashText(string text)
{
byte[] encodedBytes;
using (var md5 = new MD5CryptoServiceProvider())
{
var originalBytes = Encoding.UTF8.GetBytes(text);
encodedBytes = md5.ComputeHash(originalBytes);
}
return Encode(encodedBytes);
}
While this other function would be a Helper ?
public static string Encode(byte[] byteArray)
{
if (byteArray == null || byteArray.Length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteArray.Length; i++)
{
sb.Append(byteArray[i].ToString("x2"));
}
return sb.ToString();
}
How about a Extension could you provide any simple examples of when I should turn something into a extension of a method ?
When to make a method a static helper method or an extension method is a subjective decision. If you need help deciding whether or not to make a method an extension method, then ask yourself whether it makes sense to include that method in the other class as a normal instance method.
For example, you might have a method like this:
Does this make sense as an instance method of the
Stringclass?Design-wise this polutes the
Stringclass with a completely unrelated class. It makes theStringclass a “kitchen sink” that collects anything and everything remotely related to strings.On the other hand, suppose you have method like this:
Does this make sense as an instance method of the
Stringclass?Absolutely! This an natural extension to the existing
Splitfunctionality. It could have been included in the original implementation ofString.Everything in between can be judged on a case-by-case basis.