System.String
or System.IO.File
.To make an extension method, make the method static and put the
this
keyword in the front of the type you're extending as the first parameter, like so.public static class ExtensionMethods { public static void RemoveWhitespace(this string text) { return text.Replace(" ", "") .Replace("\t", "") .Replace("\n", ""); } }And this is how you would call string.RemoveWhitespace
string text = " a string "; text.RemoveWhitespace(); // text == "astring"Really all that is happening here is you're using a different/nicer syntax to call regular method. Instead of calling some method
static string RemoveWhitespace(string text)
, you're calling it on the instance of string you want to modify.