C# Extension Methods

As defined by MSDN: Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. It was introduced in .NET 3.5 at the same time with LINQ – which in turn uses Extensions. That’s why all of a sudden, you can now use LINQ against Enumerables even though those objects where never modified.

As defined by me: A very powerful feature that sometimes new developers forget, doesn’t care or doesn’t know about it or don’t quite understand how to create one.

Consider the following scenario. To secure your application credentials at runtime, you decided to use System.Security.SecureString. The content of the SecureString is encrypted while in memory. Read the documentation about SecureString. You cannot assign a string directly to SecureString. It only has a method of AppendChar that accepts one character at a time. So to assign a string, you have to do this.

string s = "MyPassword";
System.Security.SecureString secureString = 
  new System.Security.SecureString();
foreach(char c in s)
{
    secureString.AppendChar(c);
}

Imagine you have to do this over and over in different classes in your code. So you decided to create a public static method and call it every time like this.

public static System.Security.SecureString GetSecureString(string s)
{
    System.Security.SecureString secureString = new System.Security.SecureString();
    foreach (char c in s)
    {
        secureString.AppendChar(c);
    }
    return secureString;
}
static void Main(string[] args)
{
    string unsecuredPassword = "Password123";
    var securePassword = GetSecureString(unsecuredPassword);
}

How about doing it in a more elegant way. Like for example, when you convert the string into lower case, you would do this.

string myString = "Hello World";
string lower = myString.ToLower();

And to convert to SecureString, you would do this.

string unsecuredPassword = "Password123";
var securePassword = unsecuredPassword.ToSecureString();

If you add ToSecureString() into your existing string, it will not compile. Because ToSecureString() is not a method or extension of string by default. We have to create an extension method to be able to do this.

To create an extension

  1. Create a public static class
  2. Create a public static method with the parameter you wish to extend. In this example,  the string object.
  3. Put the this keyword before the object you are extending.
  4. Don’t forget the this

See example below.


public static class StringExtension
{
   public static System.Security.SecureString ToSecureString(this String s)
   {
       System.Security.SecureString secureString = 
       new System.Security.SecureString();
       foreach(char c in s)
       {
           secureString.AppendChar(c);
       }
       return secureString;
   }
 }

And now, to use it…

    

 class Program
 {
     static void Main(string[] args)
     {
         string unsecuredPassword = "Password123";
         var securePassword = unsecuredPassword.ToSecureString();
         Console.Read();
     }
 }
        

Creating extension methods is easy after all. But don’t overdo it! Enjoy coding!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.