Skip to main content

Extension Methods in C#

In this post, we will discuss what are extension methods?  how to create extensions methods? and demonstrate the use of extension methods with an example.

What are extension methods in c#?

  • Extension Methods are static methods, but they are called as if they were instance methods on the extended type.
  • In addition, they provide us an ability to add methods to existing types without creating a new derived type or modifying the original type.
  • These were first introduced in C#3.0.
Extension Methods in CSharp #extension #methods #csharp Click To Tweet

Whenever you make any method as an extension method it will have an arrow icon as shown below:

Extension Methods in C#

How to create extension methods?

Creating extension method involves two steps:

  1. Make the class static whose method you want to be extended.
  2. Precede ‘this‘ modifier in the method parameter. The type of the first parameter will be the type that is extended.

Extension method with an example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "dotnetcrunch";
            string newName = name.MyExtensionMethod();
            Console.WriteLine(newName);
            Console.ReadKey();
        }
    }

    public static class StingHelper
    {
        public static string MyExtensionMethod(this string input)
        {
            char[] charArray = input.ToCharArray();
            charArray[0] = char.IsLower(charArray[0])?char.ToUpper(charArray[0]):char.ToLower(charArray[0]);
            return new string(charArray);
        }
    }
}

Output produced by above code

Extension Methods in C#

Extension Methods in .NET Framework

There are many extension methods available in the .NET Framework which we use while working with LINQ. These extension methods were written by Microsoft and are available in all C# programs targeting recent versions of the .NET Framework.

Note: On most of the extension methods, you need to add the using System.Linq directive to the top of the code.

These are some of the commonly used extension methods of LINQ

  • StartsWith
  • Average
  • GroupBy
  • OrderBy
  • Min
  • Max
  • Skip
  • Take

Points to remember

  • An extension method will never be called if it has the same signature as a method defined in the type.
  • Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using ‘Extensions;‘ directive.
  • At compile time, extension methods always have lower priority than instance methods defined in the type itself.

To know more about C# features with respect to its versions, navigate to the below post.

List of C# features version by version

We hope that with the help of this article you were able to understand Extension Methods in C# 3.0 and above.

What do you think?

Dear Readers,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you.

Happy Coding!

Was this article worth reading? Share it with fellow developers too.

DotNetCrunch

DotNetCrunch is a popular blog for latest Microsoft® technologies which is aimed for beginners and intermediate level professionals.

25