I'm not entirely sure if I've mentioned this before BUT I happen to LOVE extension methods.
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
I recently had to write some of them up for Formating TFN and ABNs.
Here are the TfnAbnExtnesion Methods. (I could have also used regular expression to accomplish this but I chose to keep it simple/maintainable.)
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
I recently had to write some of them up for Formating TFN and ABNs.
Here are the TfnAbnExtnesion Methods. (I could have also used regular expression to accomplish this but I chose to keep it simple/maintainable.)
public static class TfnAbnExtensions { /// <summary> /// Formats the TFN. /// If the length is 9 char, return XXX XXX XXX /// If the length is 8 char, return XXX XXX XX /// </summary> /// <param name="tfn">The TFN.</param> /// <returns></returns> public static string FormatTfn(this string tfn) { // Ignore null/empty strings if (string.IsNullOrWhiteSpace(tfn)) { return string.Empty; } // Remove any existing whitespaces var newtfn = tfn.UnFormatAbnTfn(); var result = string.Empty; var tfnCharArray = newtfn.ToCharArray(); for (int i = 0; i < tfnCharArray.Length; i++) { if (i % 3 == 0 && i != 0) { result += " "; } result += tfnCharArray[i].ToString(CultureInfo.InvariantCulture); } return result; } /// <summary> /// Formats the ABN. /// </summary> /// <param name="abn">The ABN.</param> /// <returns></returns> public static string FormatAbn(this string abn) { // Ignore null/empty strings if (string.IsNullOrWhiteSpace(abn)) { return string.Empty; } // Un-format the ABN var newtfn = abn.UnFormatAbnTfn(); var result = string.Empty; var abnCharArray = newtfn.ToCharArray(); var tempCounter = 0; for (int i = 0; i < abnCharArray.Length; i++) { // Second character & then every 3rd char if (i == 2 || tempCounter == 3) { result += " "; tempCounter = 0; } tempCounter++; result += abnCharArray[i].ToString(CultureInfo.InvariantCulture); } return result; } /// <summary> /// Un formats the ABN or TFN by removing blank spaces /// </summary> /// <param name="abnTfn">The ABN or TFN.</param> /// <returns></returns> public static string UnFormatAbnTfn(this string abnTfn) { return string.IsNullOrWhiteSpace(abnTfn) ? abnTfn : abnTfn .Replace(" ", string.Empty) .Replace("-", string.Empty) .Replace("/", string.Empty); } }
References: Extension Methods (C# Programming Guide)