Iqueryable where string predicate pronouns

property - ref extension method c #

Use inheritance whenever possible instead of extension methods .

to edit

I prefer to keep this short and simple, but of course I'll answer the following questions.

In those cases where inheritance is possible, that is, classes that are not sealed, it is almost always a better option than extension methods. In fact, that's what the best practices document that womp has referenced. It has headings like "Be careful with extension methods," "Think twice before extending types you don't own," and "Prefer interface extensions over class extensions". In other words, it just says what my one-liner went into more explicitly.

The article gives detailed reasons, but the bottom line is that this is how the extension methods were developed. They were added to the language late in the game as a bit of syntactic sugar to allow MS to wedge itself into LINQ without going back and reinventing the wheel. This is the canonical example of what they're good for. Another good example is adding helper methods like:

Note that in this case, extension methods were the only way to achieve this additional feature because strings are sealed.

As for the composition on inheritance, this is a truism, but I don't see the relevance here. Regardless of whether we are using extension methods or inheritance, the goal is to change the interface to allow a different method. How this method is implemented, whether through composition, generic, or some other technique, is orthogonal.