Home  >  Article  >  Backend Development  >  C# Extension Methods

C# Extension Methods

WBOY
WBOYOriginal
2024-09-03 15:32:56594browse

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.

Syntax of C# Extension Methods

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.

Working of C# Extension Methods

  • The Extension methods are the custom methods that are created additionally and are not part of the original class.
  • A namespace is defined within which a static class is defined and then the extension method is defined within the static class. By using the namespace within which the extension method is defined, the method can be used throughout the application.
  • The extension method is a special case of the static method defined inside a static class whose first parameter is the type of the operator it is going to operate on with a prefix this keyword.
  • The extension methods can be included in .NET framework classes, custom classes, structs or interfaces, third-party classes.

Examples to Implement C# Extension Methods

Below are the examples of C# Extension Methods

Example #1

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:

C# Extension Methods

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.

Example #2

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:

C# Extension Methods

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.

Example #3

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:

C# Extension Methods

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.

Conclusion

In this tutorial, we understand the concept of C# Extension Methods through definition, its syntax, and working through programming examples and their outputs.

Recommended Article

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 –

  1. Random Number Generator in C#
  2. Static Constructor in Java
  3. TextWriter in C#
  4. Static Constructor in C#

The above is the detailed content of C# Extension Methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# Pattern MatchingNext article:C# Pattern Matching