Home > Article > Backend Development > C# Extension Methods
As per the literal meaning of extension, the additional methods are called C# Extension Methods using which the additional methods can be added without doing any changes or inheriting or restructuring the original struct, class, or interface and we can add such extension methods to the custom classes created by us, the classes of .NET framework or to the classes from third party or interfaces and these extension methods are accessible throughout the flow of the program by including the namespaces in which they are defined and it is a static method of a special kind defined in a static class.
Defining the namespace, class, and extension method.
Syntax:
namespace namespace_name { public static class class_name { public static bool extension_method_name(parameters_list) { //Blocks of code } } }
Where namespace_name is the name of the namespace within which the extension method is defined.
Class_name is the name of the static class in which the extension method is defined.
Extension_method_name is the name of the extension method.
The parameter list is the list of parameters with the first parameter being the type of the operator the method is going to operate on which will have a prefix this keyword.
Below are the examples of C# Extension Methods
C# program to demonstrate the Extension method in a program to compare two integers:
Code:
using System; using System.Text; //a namespace called check is defined namespace check { // a static class called extensionclassmethod is defined public static class extensionmethodclass { //extension method to compare two integers is defined public static bool extensionmethodname(this intstr, intval) { return str>val; } } //a class called check1 is defined class check1 { //main method is called static void Main(string[] args) { intstri = 565; //extension method defined in another static class is called here bool z = stri.myExtensionMethod(200); Console.WriteLine("The result of the comparison is: {0}", z); Console.ReadLine(); } } }
Output:
Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to compare two integers is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the result of the comparison of two integers. The output of the snapshot is as shown in the snapshot above.
C# program to demonstrate the Extension method in a program to find out the length of a string:
Code:
using System; using System.Text; //a namespace called check is defined namespace check { // a static class called extensionclassmethod is defined public static class extensionmethodclass { //extension method to find out the length of a string is defined public static intextensionmethodname(this string str) { return str.Length; } } //a class called check1 is defined class check1 { //main method is called static void Main(string[] args) { string stri = "ShobhaShivakumar"; //extension method defined in another static class is called here int z = stri.extensionmethodname(); Console.WriteLine("The length of the string obtained by using extension method is: {0}", z); Console.ReadLine(); } } }
Output:
Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to compute the length of the string passed as a parameter to it, is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the length of the string passed as a parameter to it as the result. The output of the snapshot is as shown in the snapshot above.
Code:
using System; using System.Text; //a namespace called check is defined namespace check { // a static class called extensionclassmethod is defined public static class extensionmethodclass { //extension method to add two numbers is defined public static intextensionmethodname(this intstr, intval) { return str+val; } } //a class called check1 is defined class check1 { //main method is called static void Main(string[] args) { intstri = 100; //extension method defined in another static class is called here int z = stri.extensionmethodname(200); Console.WriteLine("The result of addition of two numbers obtained by using extension method is: {0}", z); Console.ReadLine(); } } }
Output:
Explanation: In the above program, a namespace called check is defined. Then a static class called extension method class is defined within which the extension method to add two numbers passed as a parameter to it, is defined. Then another class called check1 is defined within which the extension method can be added even though it is defined in a different class but comes under the same namespace. The extension method returns the result after adding the two numbers.
In this tutorial, we understand the concept of C# Extension Methods through definition, its syntax, and working through programming examples and their outputs.
This is a guide to C# Extension Methods. Here we discuss the Introduction to C# Extension Methods and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –
The above is the detailed content of C# Extension Methods. For more information, please follow other related articles on the PHP Chinese website!