c sharp

C# Extension Methods

The extension method concept is utilized to insert new methods in the class already present in the program. Or it can allow us to add some extension features without editing the source code of the original type. This prevents the use of inheritance and makes the code complicated. How can these methods be accessed if the extension methods are present in the new class?

The response to this query lies in the use of a binding parameter. These are used to bind the class with the newly created functions. It is an empty variable that does not contain any value; it just acts as a simple keyword. It is always declared in the first place in the list of parameters. If it is written other than in the initial place, then the relevant compiler will give an error. The keyword used for the binding purpose is ‘this’.

# This sample s;

All extension methods are always declared as Static, but once the binding has occurred with the class, these functions become non-static.

Example 1

Consider a simple C sharp program in which three functions are defined. All these functions contain a simple console.writeline statement to display the message on the screens. These methods are the regular functions like simple programs have.

# Public void M1()

Now we want to add extensions to this existing program. Extensions play an important role in adding new functions in class without using inheritance. Moreover, we can simply add a new class without editing the previous one. We will add two extension methods to the class sample. The new functions will be declared in a new static class and become part of the existing sample class. This process is known as binding. So a new static class is created. This class has no link with the existing class, as it is not inherited from the abstract class or already existing class.

Inside the class, we declare two new static functions. These functions have the same statements to be displayed. While the declaration of the static functions, in the parameter, we use the keyword ‘this’ along with the object of the sample class. A ‘this’ is a binding parameter; it does not contain any value but is only used for binding the newly created static methods with the existing one in the class already defined. As we want to bind the methods with the class sample, the class and the object are used in arguments for the binding purpose.

# Public static void M4 (this sample s)

Similarly, the second function will also take this keyword and the object of the class. In the list of parameters, we declare binding parameters always at the start.

This function will display a statement passed to it through the main program. A class for the main program is declared. Inside the main program, we will create an object for the sample class, as we have linked the new methods with the sample class, so all these methods will be accessed through the object of the sample class. There is no need to create an object for the new class.

# Sample s = new sample ();

The object of the class is stated dynamically. This object will call all the functions of both the separately defined classes.

# S.M1 ();

We will execute the above source code in the Ubuntu terminal. For this purpose, a compiler will be needed, so we use MCS, and we will use mono to execute the file with the extension of .exe.

$ MCS file.cs

$ mono file.exe

Example 2

In this example, a Boolean function is created to compare two values and return whether the first value is greater than the first one. This comparison is made in the extension class declared in an extension method. After declaring the libraries, we have directly used a single class in which an extended method is declared.

Inside the class, we have taken a bool type function. This Boolean will return a single value, ‘True’ or ‘False’ at a time. If the first value is greater than ‘True’ will be sent; otherwise, the answer will be False. This parameter contains the ‘this’ parameter to bind the extended function with the variables we have sent to it from the main program. Whereas two variables of the integer data type are also declared here to store the values. The comparison between these two integer numbers will be made, and the value will be returned.

Inside the main program, an integer type variable is taken and assigned a value.

# Bool z = stri.myExtensionMethod(200 );

A bool type variable will be declared here to accept the function’s returned value in a function call. The object stri is used to call the function. This will take a value in the parameter. The second value is sent to it through the object when calling the function.

Save the code and execute it; you will see that the first value was smaller than the second one, which is why the answer is False.

Example 3

In this example of C sharp, we have used a built-in string method to calculate the input string’s length passed towards the main program’s function. A static class is created. The static keyword shows that another class cannot inherit it. Inside this static class, a static method is declared. This method will find the total length of the string. So the method parameter will contain a string type variable to accept the input string.

The length method is accessed through the object of the string. Now declare the main function. Assign a string value to the string data type.

# Int z = stri.extensionmethodname();

This function call will take the length back and store it in the integer type variable. This length will be displayed through this variable later with the help of the writeline() method.

Compile the code and execute it. You will see that A string of 10 characters was used in the program.


An extension method in C sharp is used to add some extra functionality to the program without modifying the existing code once written. This can be done by using an extra class with extended methods. However, these methods are part of another class but can be linked and accessed from the object of the already created class. We have used some basic examples that have explained the creation and working of the extended methods. The keyword ‘this’ plays an important role in binding extension methods with the class. We use this keyword in the parameter of the extended function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.