DEV Community

Saravanakumar R
Saravanakumar R

Posted on

Extension Methods

  • In C#, extension methods are like adding these new cool tricks to something that already works fine.
  • You donโ€™t have to change the original class, you just add a little helper on the side.

Basic example
Imagine you have a toy car. Now, this toy car can already do a lot of cool things like moving forward, backward, or turning. But letโ€™s say you want your toy car to fly or glow in the dark, and the toy already works great without those things. Instead of building a brand-new toy car, you just add extra features to the one you already have.



public class Backpack
{
    public List<string> Items { get; set; } = new List<string>();
}

public static class BackpackExtensions
{
    public static int GetItemCount(this Backpack backpack)
    {
        return backpack.Items.Count;
    }
}
Enter fullscreen mode Exit fullscreen mode

The Backpack is a class (something that exists already).
Checking fullness is the extension method (the new feature you add).

Interview Prep

  1. What are extension methods in C#?
    Extension methods allow you to "add" methods to existing classes or interfaces without modifying their source code or creating a new derived type.

  2. Why do we use extension methods?
    To extend the functionality of classes, especially when you can't or don't want to modify the original class (e.g., third-party libraries).

  3. What are the key characteristics of extension methods?

  • Defined as static methods in a static class.
  • Use the this keyword before the first parameter to specify the type they extend.
  1. How do extension methods differ from instance methods?
    Instance methods are defined within a class, whereas extension methods are defined externally but appear as if they belong to the class.

  2. What is the this keyword in the context of extension methods?
    The this keyword before the first parameter indicates the type the method is extending.

  3. Can an extension method take additional parameters apart from this?
    Yes, additional parameters can be added after the this parameter.

  4. Can you overload extension methods?
    Yes, you can overload extension methods by defining multiple methods with the same name but different parameter signatures.

  5. What happens if an extension method has the same name as an instance method?
    The instance method takes priority, and the extension method is ignored.

  6. Can extension methods be used on sealed classes?
    Yes, extension methods are often used to add functionality to sealed classes like string or DateTime.

10.Can you use extension methods with generic types?
Yes, you can create generic extension methods to work with any type:
public static T GetDefaultValue<T>(this T obj) => default;

11.Can you use extension methods for LINQ operations?
Yes, LINQ heavily relies on extension methods for methods like Where, Select, etc.

12.What are some real-world examples of using extension methods?
string.IsNullOrWhiteSpace().

Top comments (0)