0

We're currently working on a Web-API project written in C#. It contains a lot of static outer classes that have non-static nested classes. Something like

public static class OuterClass
{    
    public class InnerClass : ParentClass
    {
            
    }
}

This pattern is used regularly throughout the project, hence my question. Are there any pitfalls in using a static outer class that has a non-static nested class? Let alone, using a lot of them throughtout the application?

  • Does the static class have any other thing apart from inner classes? – Chetan Jan 03 '21 at 10:40
  • 2
    I think this is an example of "static classes as namespaces" anti-pattern – Charlieface Jan 03 '21 at 12:47
  • The static outer class has no members. It has just _public_ inner classes. But these have members (that is, fields, properties, methods, and so on). – user14699123 Jan 03 '21 at 13:04
  • 1
    It would be helpful if you'd say *why* you are doing this strange thing. It's not necessarily wrong but it is sufficiently odd that I'd want a solid justification if asked to review this code. – Eric Lippert Jan 04 '21 at 05:59
  • @EricLippert, I am not the author of the code so I cannot do justice to the explanation why that choice was made. But, after having spoken with the author, I understand the following. The outer static class is a way to implement an API resource in a [Vertical Slice Architecture](https://jimmybogard.com/vertical-slice-architecture/).The request's Query, Validator, Result, Handler, and so on, are each implemented using a public non-static inner class. – user14699123 Jan 04 '21 at 08:43
  • Have you learned anything from the previous instance of the same question? https://stackoverflow.com/questions/65519928/static-outer-class-having-inner-class-what-are-the-advantages-and-pitfalls – CodeCaster Jan 04 '21 at 22:41
  • 1
    Thanks for that sketch; that's helpful. The question I would ask when critiquing this design choice is: what compelling benefit over namespaces does this static class approach yield? I don't see any harm offhand but if there is no benefit over doing it using namespaces as they were designed, then I would typically follow the standard design pattern. One typically only uses static classes for storing globally useful methods, and particularly extension methods. – Eric Lippert Jan 06 '21 at 17:33

1 Answers1

1
  • If there are only inner classes, but not also fields and methods, then namespaces may be preferred instead of those outer public static classes.
    • You can define fields and methods in a static class (or class in general) but not a namespace
    • The using keyword is a convenient way to import all of the accessible types/classes defined in a namespace, so that you do not need to fully qualify them.
    • Otherwise, if you intend to add members/methods, then it makes sense to keep those as classes.
  • Especially if the project will be used as a library, you should still use namespaces to avoid collisions in the global namespace.
ELinda
  • 2,658
  • 1
  • 10
  • 9
  • Regarding the `using` keyword: For static classes, you can use the [using static directive](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static). – Brian Jan 04 '21 at 14:10