Extension Method vs. Helper Class

39,160

Solution 1

Creating an extension method means that it will automatically show up during intellisense when a user uses that type. You have to be carefull not adding a lot of noise to the list of methods developers browse (especially when creating a reusable framework). For instance, when those methods are just usable in a certain context, you are probably better of using 'normal' static methods. Especially when implementing extension methods for general types such as string.

Take for instance an ToXml(this string) extension method, or an ToInt(this string) extension methods. Although it seems pretty convenient to have these extension methods, converting text to XML is not something you will do throughout the application and it would be as easy to have do XmlHelper.ToXml(someString).

There is only one thing worse, and that is adding an extension method on object.

If you're writing an reusable framework, the book Framework-Design-Guidelines by Krzysztof Cwalina is an absolute must read.

Solution 2

The question is which .NET Framework will you target? If < 3.5 then extension methods are not available. Otherwise, why would you create a new class?

Solution 3

I prefer Extension Method, because your code is elegant, and you can define an extension method on a sealed class of the framework.

Solution 4

An extension method is automatically part of a static class. This means that the consumer can use either the extension methods or call the static method from the class if she wants it. I use extension methods as much as I can, they are easier to discover if they're put in the proper namespace.

Solution 5

Extension methods allow developers to not be aware exactly what the helper class is called and where it is located, not to mention the very fact of its existence. Do note, that you still need to put their namespace in using clause - perhaps place them in some common, top-level namespace for your application.

Share:
39,160

Related videos on Youtube

Matthew Layton
Author by

Matthew Layton

Passionate about programming, technology and DLT.

Updated on January 25, 2020

Comments

  • Matthew Layton
    Matthew Layton over 4 years

    Possible Duplicate:
    Extension Methods vs Static Utility Class

    I am building an API of general functions which perform actions based upon objects in .NET. For example; I have created a function that checks a string to see if it is an email address.

    I could either have:

    static bool IsEmailAddress(string text)
    {
        return IsMail(text);
    }
    

    or I could create an extension method that would be used like so:

    string text = "[email protected]";
    if (text.IsEmailAddress())
    {
    }
    

    which is more suitable, or do you think since this is a general purpose library, I could technically implement it both ways and allow the developer to decide which is best for them?

    • Mahmoud Gamal
      Mahmoud Gamal almost 12 years
      BTW: use return IsMail(text) instead of the if statement.
    • Andre Calil
      Andre Calil almost 12 years
      I'd go with extension methods AND the refactoring Mahmoud suggested.
    • CodesInChaos
      CodesInChaos almost 12 years
      I think it's not the responsibility of string to know what an email address is. So I don't think this should be an extension method.
    • Lubo Antonov
      Lubo Antonov almost 12 years
      You are soliciting opinions, which I believe is discouraged here.
    • SteveCinq
      SteveCinq almost 6 years
      @CodesInChaos Tho I agree with you in principal, in the context of the OPs code, it is a responsibility that he's impressing on string. And an extension method is nice and clean and concise, so I'd go for that in this case.
  • Yuriy Anisimov
    Yuriy Anisimov over 7 years
    Don't forget that extension is bound to namespace - so until you don't specify a namespace of extension in your client class - intellisense for your extension's methods won't appear. Another thing what extension really is? - after compilation - it's static class with static method - so I'd rather use extensions as syntactic sugar and have a good structure of my namespaces rather than creating StaticHelpers.