Extension Methods in C# 3.0

Extension Methods in C# 3.0 for Complete Noobs

In this post, we will discuss what are extension methods in C#?  how to create extension methods in C#? 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 the 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.

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

Extension Methods in C#

How to create extension methods in C#?

Creating an 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 in C# with an example

In this example, we have written an extension method that checks if the first character of the input string is a lowercase letter then converts it to uppercase.

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
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 the code above

Extension Methods in C#

Extension Methods in .NET Framework

Now we know how to create our own Extension Methods in C#. Let’s look at the extension methods that are offered by the .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: For 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

👉 Expression Trees in C#

📢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 using the ‘Extensions;’ directive.
  • At compile time, extension methods always have lower priority than instance methods defined in the type itself.

EXTENSION METHODS IN C#

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

List of C# features version by version

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

Microsoft .NET Framework

co preview img 81 1563523386

⭐C# Mastery – Mosh Hamedani

One of the best courses to master your C# skills right from level zero. This series is divided into 4 sections:

  1. C# Basics
  2. C# Intermediate
  3. C# Advanced
  4. Unit Testing for C# Developers

csharp mastery course mosh

🤷‍♀️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 </>

✌ You may connect with me on Instagram

Share with your friends:

1 thought on “Extension Methods in C# 3.0 for Complete Noobs”

  1. Pingback: C# Version History: Detailed List of C# Features From Version 1 to 9 - DotNetCrunch

Leave a Comment

Your email address will not be published. Required fields are marked *