---
title: "What are extension methods and how are they used?"  
description: "What are extension methods and how are they used?"  
author: "Anubhav Sharma"  
published: 2025-06-10  
updated: 2025-06-12  
canonical: https://www.mindstick.com/forum/161701/what-are-extension-methods-and-how-are-they-used  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# What are extension methods and how are they used?

**What are [extension methods](https://www.mindstick.com/forum/160389/how-to-sort-elements-in-a-collection-using-the-linq-extension-methods) and how are they used in C#?**

## Replies

### Reply by ICSM Computer

> **[Extension](https://www.mindstick.com/articles/22/extension-method) [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best)** allow you to **"add" methods to existing types** (classes, structs, interfaces) **without modifying their source code** or creating a derived type.

They are a type of **syntactic sugar** in C# that enables more natural, readable code.

## Key Features

- Declared as **static methods** in a **static class**
- First parameter is prefixed with the `this` keyword
- Can be called using **instance method syntax**

## Example: Creating an Extension Method

```cs
public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string input)
    {
        return string.IsNullOrEmpty(input);
    }
}
```

Now you can use it like this:

```cs
string name = "";
bool result = name.IsNullOrEmpty();  // Looks like a real method of string!
```

## How It Works Behind the Scenes

The compiler converts this:

```cs
name.IsNullOrEmpty();
```

Into this:

```cs
StringExtensions.IsNullOrEmpty(name);
```

So the method is **still static**, just looks like an instance method.

## Practical Example: List Extensions

```cs
public static class ListExtensions
{
    public static void PrintAll<T>(this List<T> list)
    {
        foreach (var item in list)
            Console.WriteLine(item);
    }
}
```

Usage:

```cs
var nums = new List<int> { 1, 2, 3 };
nums.PrintAll();  // Extension method used like a built-in method
```

## When to Use Extension Methods

| Use Case | Use Extension Method? |
| --- | --- |
| Want to add methods to built-in types | Yes |
| Cannot modify source of the type | Yes |
| Need to enhance readability or reuse | Yes |
| Need polymorphism or override behavior | No (use inheritance instead) |

## Limitations

- Cannot override existing methods
- Cannot access private members of the target type
- Only resolved at **compile time**, no virtual dispatch

## Common Uses

- LINQ (e.g. `Where`, `Select`, `ToList`)
- Fluent APIs
- Utility functions for built-in or library types

## Example: LINQ Extension Method

```plaintext
var even = numbers.Where(n => n % 2 == 0);
```

Here, `Where()` is an **extension method** defined in `System.Linq.Enumerable`.


---

Original Source: https://www.mindstick.com/forum/161701/what-are-extension-methods-and-how-are-they-used

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
