Reputation: 29
I am recently learning about c# and read that the extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. What are some examples to clarify this?
Upvotes: 1
Views: 10304
Reputation: 123
An extension method in C# allows you to add functionality to an existing class or interface without modifying the class or interface itself. It is a static method that is defined in a static class and is called as if it were an instance method of the extended class or interface.
The main benefits of using extension methods are
Extending functionality: Extension methods allow you to add new functionality to existing classes or interfaces without modifying them. This can be useful when you want to add a new feature to a class that you don't have access to modify, such as a third-party library.
Code re usability: Extension methods can be reused across different projects and classes, which can save you time and effort in writing similar code.
Readability: Extension methods can make your code more readable and concise by encapsulating complex logic into a single method call.
Here is an example of an extension method:
public static class StringExtensions
{
public static string Reverse(this string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
In this example, we have defined an extension method called Reverse for the string class. The method takes a string as an input and returns the reverse of the string.
To use this extension method in your code, you simply need to call it as if it were an instance method of the string class, like this:
string originalString = "Hello, world!";
string reversedString = originalString.Reverse();
In this example, the Reverse method is called on the originalString object, which is an instance of the string class. The method returns a new string, which is assigned to the reversedString variable.
Overall, extension methods can be a powerful tool for extending and customizing the behavior of your code without modifying existing classes or creating new subclasses, and they can help make your code more readable and reusable.
Upvotes: -3
Reputation: 36
Extension methods are special static methods in a static class which enables us to add new methods to built-in types and custom types without creating new derived type. Which provides a user defined custom functionality just like built-in methods have. For Example, String has ToLower(), ToUpper() methods. In same way we can create our own method.
Let's check following example, we have created Remove() method which removes any given character/string from original string.
public class Program
{
public static void Main()
{
string sample1 = "This, is a, sample string";
Console.WriteLine("output string : " + sample1.Remove(","));
}
}
public static class SampleExtensions
{
public static string Remove(this string str1, string InputStr)
{
return str1.Replace(InputStr,"");
}
}
Upvotes: 1
Reputation: 48726
Extension Methods allow an existing type to be extended with new methods without changing the definition of the original. It allows you to add functionality to a type that you may not be able to inherit (because it's sealed
) or you don't have the source code.
Here is an example. We will add an Age()
method to DateTime
:
public class Program
{
public static void Main()
{
DateTime birthdate = new DateTime(1980, 10, 7);
Console.WriteLine("Your age is: " + DateTime.Now.Age(birthdate));
}
}
public static class Extensions
{
public static int Age(this DateTime date, DateTime birthDate)
{
int birthYear = birthDate.Year;
int currentYear = DateTime.Now.Year;
return currentYear - birthYear - 1;
}
}
You can see from the example that we added a new Age()
method to C#'s DateTime
type. Hopefully this clears things up.
Upvotes: 7