LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Extension Methods in C# – Extend Existing Classes

A derived class is a class that inherits from another class. Extension methods make existing types easier to work with. You will commonly see them in LINQ, string helpers and reusable utility code.

1What Are Extension Methods?

Hi friends, in this article we will learn about extension methods in C#. They allow us to add new methods to an existing class without modifying its original source code or creating a derived class. A derived class is a class that inherits from another class. Extension methods make existing types easier to work with. You will commonly see them in LINQ, string helpers and reusable utility code.

2A Useful String Helper

Imagine you are building a shopping application. Different screens need to check whether a product name contains useful text. Instead of repeating the same logic everywhere, you can create a helper method.

With an extension method, you can call that helper directly on a string, almost like it was already part of the string class.

Remember An extension method adds convenient syntax. It does not actually modify the original class.

3Rules Behind Extension Methods

1. Use a static class

An extension method must be declared inside a non-nested static class.

2. Add the this keyword

The first parameter uses this followed by the type you want to extend. This identifies the type that receives the method.

3. Call it like an instance method

An instance method is called on an object. Extension methods use the same calling style, even though they are static methods.

4. Namespace matters

The namespace containing the extension class must be in scope, usually through a using directive.

Normal static call Extension call
TextHelper.HasText(name) name.HasText()
Pro Tip Keep related extension methods together in clearly named static classes, such as StringExtensions.

4Create Your Own Extension Method

Here we create HasText() for the string type. It checks whether a string contains a non-whitespace character.

Program.csC#
using System;

static class StringExtensions
{
    public static bool HasText(this string value)
    {
        return !string.IsNullOrWhiteSpace(value);
    }
}

class Program
{
    static void Main()
    {
        string product = "Laptop";
        string empty = "   ";

        Console.WriteLine(product.HasText());
        Console.WriteLine(empty.HasText());
    }
}
Common Mistake Do not forget this on the first parameter. Without it, the method is just a normal static method, not an extension method.

5See the Program Result

Laptop contains text, so the first call returns true. The second string contains only spaces, so it returns false.

Output
True
False

6Watch the Method in Action

Follow the extension class, its this parameter and the calls made on the string variables.

Program.cs - Visual Studio Code
▣⌕⑂▤
● Program.cs
 
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
 
Ln 1, Col 1UTF-8  C#
Preparing the extension method...

7Key Points to Remember

Extension methods help keep reusable logic close to the type it works with. They improve readability without changing the original class.

  • Declare them inside a static class.
  • Use this on the first parameter.
  • Make the extension namespace available to call the method.

8Interview Questions to Practise

Q1Easy

What is an extension method in C#?

Answer: It is a static method that can be called using instance-method syntax on an existing type, without modifying that type.

Q2Easy

Where must an extension method be declared?

Answer: Inside a non-nested static class.

Q3Medium

Why do we use this in the first parameter?

Answer: It identifies the type being extended and makes the method available through that type's instance syntax.

Q4Medium

Can extension methods access private members?

Answer: No. They follow normal access rules and cannot access private members of the extended type.

Q5Hard

What happens if an instance method has the same name?

Answer: The instance method takes precedence over an extension method during method resolution.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.