find and solve || findandsolve.com
Please wait.....

Extension methods in C#


Extension methods, introduced in C# 2008, allow you to extend a class without deriving a new class from that class. The behavior of extension methods is similar to that of static methods and they are also defined with the static keyword.
An extension method can be defined in a static class. The first parameter of an extension method is of the type that the extension method extends and it is preceded by this keyword, as shown in the following code snippet:

using System;
namespace extensionMethods
{
public static class MyClass
{
 public static int MyExtensionMethod(this string number)
 {
return Int32.Parse(number);
  }
}
public class Program
{
public static void Main()
{
int num1=200;
int num2;
int result;
string x="300";
num2=x.MyExtensionMethod();
result=num1+num2;
Console.WriteLine("Result ="+result);
Console.ReadLine();
}
}
}

The first given above code snippet shows the definition of a static classMyClass. In the MyClass class, we have defined an extension methodMyExtensionMethod, which extends the string type. The MyExtensionMethod method takes a string type parameter converts it into an equivalent integer value, and returns the integer value. You can notice that this keyword is used before the parameter list of the MyExtensionMethod method.


In the second code snippet, we have declared three int-type variables,num1,num2, and result, and assigned the value 200 to num1. After that, we declared a string type variable,x, and assigned it the value 300. Next, we have called the extension method, MyExtensionMethod, by using the string variable,x. The Extension Method method converts the value stored in the x variable to an equivalent integer value and returns the resultant value. We have stored the value returned by the MyExtensionMethod method in the num2 variable. After that, we added the values stored in the displayed value stored in the result variable.

In C#, an extension method is a special kind of static method that allows you to add new methods to an existing class or struct, without modifying the original code.
Extension methods are defined as static methods, but they are called as if they were instance methods on the extended type. Extension methods are declared with this keyword, followed by the type that will be extended.

Here's an example of an extension method that adds a Square method to the int type:

public static class IntExtensions
{
    public static int Square(this int i)
    {
        return i * i;
    }
}
Once the extension method is defined, it can be called on any int value as if it were an instance method:

int value = 5;
int result = value.Square(); // result = 25
You can also create extension methods for custom classes or structs, for example, you have a class Person and you want to add a method FullName which concatenates FirstName and LastName

public static class PersonExtensions
{
    public static string FullName(this Person person)
    {
        return person.FirstName + " " + person.LastName;
    }
}
You can also use extension methods to add functionality to interfaces. For example, you can create an extension method for the IEnumerable<T> interface that calculates the average of a collection of numbers:

public static class IEnumerableExtensions
{
    public static double Average(this IEnumerable<double> numbers)
    {
        return numbers.Sum() / numbers.Count();
    }
}
It's important to note that extension methods are only in scope when the namespace or the containing assembly of the extension class is in scope. Also, extension methods are considered last when looking for an appropriate method to call, after all, other methods of the class and its base classes have been considered.

Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response