Coffeehouse Post

Single Post Permalink

View Thread: C# Extension Properties?
  • User profile image
    Dr Herbie

    dahat said:
    spivonious said:
    >  If you want to add a method to a class, then subclass it.

    What if the class you want to add a method to is sealed or worse, is a struct, preventing you from doing just that? Not unlike DateTime, TimeSpan or String?
    Well, without the extension method language feature, you'd have to use the decorator pattern. If you only want to add a single method, that's potentially an awful lot of work, which is why I like extensions.

    For me the real strength of extension methods is in adding type-inferenced extension methods to interface, so that classes inheriting that interface automatically get interface-related functionality for free (we discussed this a while ago over on this thread).