Extension Method In C# - With the help of Extension Methods, We can add more functionality to existing type without modifying the original types. We can add new methods to existing types without creating new derived type, recompiling or modifying types. In simple words it extends the functionality of dot net.
Don't forget to know about one more interesting topic - Delegates in C#
We can create it by creating static class with static methods. We can call extension methods with same general rules.
Extension Method's first parameter(followed by this) specifies which type the method operates on, and the parameter should be preceded by the {this} modifier.
Extension methods are only in scope when you explicitly import the namespace into your source code with a {using} directive.
Way to create extension method.
First create an static class
namespace ExtensionMethods
{
using System;
public static class StringExtensions
{
}
}
Add an Static method
namespace ExtensionMethods
{
using System;
public static class StringExtensions
{
public static int GetWordCount(this String str)
{
}
}
}
Note: We are creating extension method for type {String} therefore first argument should be string followed by {this} directive.
GetWordCount(this String str)
it will return type {int} therefore its return type should be {int}
int GetWordCount(this String str)
This is an extension method, so its return type should be {static}.
static int GetWordCount(this String str)
This extension method can be access any where within the program, so it should be followed by {public} access modifier.
public static int GetWordCount(this String str)
Now adding functionality for extension method, Which will splits words from given string and then counts its length.
namespace ExtensionMethods
{
using System;
public static class StringExtensions
{
public static int GetWordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
Let us create an program for count no of words in a string.
Add namespace used in extension method class.
using ExtensionMethods;
Create an string variable with any string.
string s = "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.";
Invoke extension method.
int wcount = s.GetWordCount();
Lets look at complete example as follows.
namespace SWClass
{
using System;
using ExtensionMethods;
class Program
{
static void Main(string[] args)
{
string s = "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.";
int wcount = s.GetWordCount();
Console.WriteLine($"The total words in given string is: {wcount}");
Console.ReadKey();
}
}
}
OUTPUT: The total words in given string is: 23
If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.
Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.