Can There Be Private Extension Methods?

后端 未结 6 1593
Happy的楠姐
Happy的楠姐 2021-01-07 16:01

Let\'s say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that hel

6条回答
  •  感动是毒
    2021-01-07 16:28

    Is there a compelling reason for this requirement?

    That's the wrong question to ask. The question asked by the language design team when we were designing this feature was:

    Is there a compelling reason to allow extension methods to be declared in nested static types?

    Since extension methods were designed to make LINQ work, and LINQ does not have scenarios where the extension methods would be private to a type, the answer was "no, there is no such compelling reason".

    By eliminating the ability to put extension methods in static nested types, none of the rules for searching for extension methods in static nested types needed to be thought of, argued about, designed, specified, implemented, tested, documented, shipped to customers, or made compatible with every future feature of C#. That was a significant cost savings.

提交回复
热议问题