Is there a good reason why extension method classes cannot be nested?

I recently used extension methods to help make my code more free, but I constantly encounter situations where I cannot limit the exposure of the functionality that I created as I would like.

Let's say I have a code like this (a fully compiled example):

var liveSet = allitems.Where(x => x.Status == Status.Live).Select(x => x.Name);
var deadSet = allitems.Where(x => x.Status == Status.Dead).Select(x => x.Name);
var zombieSet = allitems.Where(x => x.Status == Status.Neither).Select(x => x.Name);

I would like to do it more freely, and stop repeating:

var liveSet = allitems.SelectNameWhere(x => x.Status == Status.Live);
...

But I consider this a helper method, not a true extension, so I would like to limit its use in the same way as I can a private method:

//In the same class
private static IEnumerable<string> SelectNameWhere(
                   this IEnumerable<Element> items,
                   Func<Element, bool> predicate)
...

, , , , . , , ... , , , , , , , :

var liveSet = SelectNameFrom(allItems, x => x.Status == Status.Live);
...

, , , , . , ?

+3
2

, , , ( ). , . , . .

:.

public static class Foo
{
   public static void DoStuff(this object param)
   {
   }
   public static class Bar
   {
      public static void DoStuff(this object param)
      {
      }
   }
}
+3

, , , . .

0

Source: https://habr.com/ru/post/1761899/


All Articles