7 responses to “C# Extension methods”

  1. cherouvim

    damn, this looks yummy!

  2. chris

    Extensions look good, but what happens when people start adding extensions to security classes?

  3. spiros

    Hi Chris,

    I am not a C# programmer and unfortunately I do not have a windows machine to try C# and see exactly how extension methods work.

    From my understanding I can’t see how extension methods can affect the security of a program. It would be nice if you could give a concrete example to base our discussion.

    Thank you for the comment and sorry for my late reply.

  4. Ashok

    Well, While Extension Methods (EM) provide a useful machanism to add new functionality to an existing base types, it also adds lot of confusion, if lot of extesions are added to base types like string. If developers keeping adding more methods to base classes for every single requirement just becuase it improves the readability (string email; email.IsValid()) then don’t you think these all unnessary EMs clutter the code in the IDE (intellisense)

    I think there are some good reasons we need it but extending base type is not really a good idea I guess.

    What I suggest is using/creating Money (http://www.martinfowler.com/eaaCatalog/money.html) like classes for all your requirements :)

    Regards,
    …Ashok

  5. billie

    extension methods are syntactic sugar around static method calls. The syntax fits well with ruby, but now in c# you must ask is this an extension method, or an actual method in the object? Adds confusion.

  6. spiros

    Hi Ashok,

    I agree with you that EMs can be misused and that we should try to create domain specific objects instead of just adding new methods to general purpose classes like string.

  7. spiros

    Billie,

    I don’t believe that extension methods add confusion because the user has to import them explicitly. Also you could have a visual indicator in your IDE. For example in Eclipse (I am a Java programmer) the static methods appear in italic fonts, so I suppose that in Visual Studio you could have a similar highlighting and because extension methods are actually static method they could also appear in italic fonts.