C# – Mastering the Basics - Extension Methods

In this series, I go over the very basics of the C# programming language. In todays guide, I will focus on writing custom extension methods for existing, prebuilt classes.

You can use extension methods to add custom functionality to classes that are built by others. In my very case, I’d like to extend the functionality of the String class to set its first letter to upper case. I already implemented this functionality in my sideproject CryptoFolio, be sure to check out my article on the topic!

As you can see in Microsofts documentation, extension methods are defined as static methods, but are called by using instance method syntax. This means that we will create a static method which can later be called by any object of the type String._ _While the implementation of such a method is very similar to a regular method, there is one thing you’ll have to remember when writing extension methods: the _this_ keyword before the parameter. Let’s see an example of how this works:

    public static String ToFirstLetterUpperCase(this String s)
        if (s == null)
            return null;

        if (s.Length > 1)
            return char.ToUpper(s[0]) + s.Substring(1);

        return s.ToUpper();

In the example, I've added a new class called _StringExtensions_. This is the place where I will store all of my custom exension methods for strings. As you can see, the method looks very normal, the only thing different is the previously mentioned _this_ keyword right before the parameters data type.

After creating the extension method, it can be used by simply calling _ToFirstLetterUpperCase()_ on any string:

```csharpString s = "hello world!";
// Output: Hello world!```