Creating Extension Methods
A useful feature of C# is the ability to add functionality to existing classes. For example, you might want a reverse function on the String class. You would then be able to call your new function this way: myString.Reverse().
Here is an example of an extension class.
Notice that the class and the Reverse function are both marked static. This is required to create an extension method. The parameter, this String str, is how C# knows that the String class is the one the extension applies too.
You can also create extension methods that accept a parameter.
And this can be called by, myString.ReplaceSpacesWith(“#”).
Extension methods are simple to create and useful for adding functionality to an existing class. Just remember that the extension class must be marked static and have a reference to the class you are extending. And the extension method must be static and the first parameter must contain this and the class your extending.