This is the third article of the LINQ series. In this article, we will learn what extension methods are and how to create and implement your own extension method.
Extension methods are a special type of static methods that are called as if they are an instance method of the extended type.
LINQ's statement query operators like select are implemented in the Enumerable class as extension methods on the IEnumerable<T> interface.
Look at the preceding demo, in spite of the Where() method not belonging to the Dictionary<key, value> class, we are still able to use it as if it belongs to the Dictionary class. This is possible because the Where() method is implemented as an extension method in the IEnumerable<T> interface and the Dictionary class implements the IEnumerable<T> interface.
Demo
Now let's see how to create an extension method.
In our project, we want to create a method that process a name and the first letter of the name should not be capitalized, if it is in upper case then it will change the letter into lower case.
We need to create a helper class in which we will create our extension method.
- class MyExtension
- {
- public static string ChangeCase(string Input) {
-
-
- if (Input.Length > 0) {
-
-
- char[] c = Input.ToCharArray();
-
-
- if (char.IsUpper(c[0])) {
-
-
- c[0] = char.ToLower(c[0]);
- }
-
- else{
-
-
- c[0] = char.ToUpper(c[0]);
- }
-
-
- return new string(c);
- }
-
- else {
- return Input;
- }
- }
- }
So, now we can call this method in our main method.
Run the application
We have the output we wanted. But we haven't done it the way we wanted to, using that ChangeCase method.
We don't want MyExtension.ChangeCase(Name);
We want string result = name.ChangeCase();. We want to invoke this ChangeCase() method as if it is an instance method defined within the string class.
Currently our ChangeCase() method is not an extension method and to convert this method into an extension method, we need to make some changes.
The class should be static, so what do we need to extend?
We need to extend our string class, so the string parameter should be the first parameter within this function and the other thing we need to do is to precede the type name (string) with this keyword that we are extending.
Note
We can add more than one parameter but the first parameter should always be the one we want to extend.
Now let's see if, after making these changes, our application works the way we expected.
Look at the image above, now we are able to use the ChangeCase() method as an extension method and look at the type of this method, it states extension and that proves it.
Now we don't even need to pass any parameter in this method.
Run the application
You might be thinking that without passing a parameter, how is the first letter converted into lower case?
When we invoke the ChangeCase() extension method and run the application, behind the scenes this extension method is converted into:
- string ChangedCase = MyExtension.ChangeCase(name);
The extension method is just a syntactic sugar that makes the developer's life sweeter.
In the next article we will see various Aggregate functions and the use of these functions.
Until then keep learning.
Thank you.