Hacker News new | past | comments | ask | show | jobs | submit login

Not true, it's very misleading - makes you believe the method is defined inside class body.

Also with functions you know they can't access private members, with extension methods you confuse them with real methods and have to think they can.

It's only the same thing if you know about it and without a tool you can't know, even with tool it takes some inspection.




Extension functions are syntax sugar for static methods that receive the class instance as this. They do not access private members.

Also, you need to specifically import the namespace of the extension function in order to use it, making it very clear to the reader of what is going on. It is not some hidden IDE magic, it's built into the core language.


I never said otherwise.

I said it was misleading syntax sugar because it implies that static functions defined in any namespace (extension methods) are the same as methods - which have access to internal members and are defined within class hierarchy.

If you know something is an extension method then you know it's just syntax sugar, but you can't determine that just by looking at the code.


I suspect the design of a class, if I have to worry about whether a method I'm calling is able to access private members or not.


Sure, but I've been stuck maintaining a codebase a couple of years back written by someone on fluent API hype train - exploring that codebase was a major PITA thanks to VS bugs and extension methods.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: