> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Extension Method

An extension method in C# is a static method defined within a static class that the compiler allows to be invoked using instance method syntax on a specified target type. It provides a compile-time mechanism to attach new methods to existing types—including sealed classes, interfaces, and value types—without modifying the original source code or requiring inheritance.

Under the hood, extension methods are purely syntactic sugar. The Intermediate Language (IL) generated by the compiler translates the instance-style invocation into a standard static method call, passing the invoking instance as the first argument.

## Syntax and Anatomy

To define an extension method, the following structural requirements must be met:

1. The containing class must be `static`, non-nested, and non-generic.
2. The method itself must be `static`.
3. The first parameter of the method must be prefixed with the `this` modifier.
4. The type of the first parameter dictates the type being extended.
5. While the containing class cannot be generic, the extension methods themselves *can* be generic, allowing them to operate on parameterized types.

```csharp theme={"dark"}
using System;
using System.Collections.Generic;
using System.Linq; // Required for the .Count() LINQ method

namespace ExtensionsLibrary
{
    // 1. The class must be static, non-nested, and non-generic
    public static class StandardExtensions
    {
        // 2. The method must be static
        // 3. The 'this' modifier precedes the first parameter
        // 4. 'string' is the type being extended
        public static int CountVowels(this string targetType)
        {
            if (targetType == null) return 0;
            
            // Method implementation
            return targetType.Count(c => "aeiouAEIOU".Contains(c));
        }

        // 5. The extension method itself can be generic
        public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> predicate)
        {
            foreach (T item in source)
            {
                if (predicate(item))
                {
                    yield return item;
                }
            }
        }
    }
}
```

## Invocation and Compiler Translation

Once the namespace containing the extension class is imported via a `using` directive, the method appears in IntelliSense and can be invoked as if it were a native instance method.

```csharp theme={"dark"}
using ExtensionsLibrary;

public class Program
{
    public void Execute()
    {
        string text = "Hello World";
        
        // Instance method syntax (Syntactic Sugar)
        int vowelCount = text.CountVowels(); 
        
        // How the compiler translates it at compile-time
        int translatedCount = StandardExtensions.CountVowels(text);
    }
}
```

## Method Resolution, Precedence, and Encapsulation Rules

The C# compiler follows strict rules when resolving method calls involving extension methods:

* **Instance Precedence:** The compiler will always prioritize *any* applicable instance method over an extension method. If the target type possesses an instance method that can satisfy the invocation (even if the signatures are not identical, such as through implicit type conversions or optional parameters), the compiler binds to the instance method. Extension methods cannot override or shadow existing instance methods.
* **Encapsulation:** Extension methods do not possess special access privileges to the type they are extending. Because they are external static methods, they can only interact with `public` or `internal` (if within the same assembly) members of the target type. They cannot access `private` or `protected` members.
* **Compile-Time Binding:** Extension methods are resolved at compile-time based on the *static* type of the variable, not the *runtime* type. They do not participate in polymorphism, virtual method dispatch, or overriding.
* **Namespace Scope:** Extension methods are only discoverable if their enclosing namespace is explicitly imported into the current file's scope using a `using` directive.
* **Null References:** Because extension methods are translated into static method calls, invoking an extension method on a `null` reference does not inherently throw a `NullReferenceException` at the call site. The `null` value is simply passed as the argument to the first parameter, meaning null-checking must be handled explicitly within the extension method's body.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
