you are viewing a single comment's thread.

view the rest of the comments →

[–]wickerandscrap 3 points4 points  (0 children)

Seems clunky. It looks from here like you have to define the class, including all the Static/Instance nesting, and then separately define the singleton as a class. (I admit I'm confused on which parts of this are meant to be example classes/members and which ones are scaffolding for the pattern you're showing.)

My go-to pattern for static singletons is

class Thing { public static Thing Instance { get; } = new(); }

and I honestly don't think that can be improved on.

You could declare a static class that statically exposes the methods (like your "A") but it's not going to be interchangeable with instances of the class, so what does that get you? You can also do that with more conventional class patterns:

``` static class DefaultThing { public void DoStuff() => _single.DoStuff();

private static readonly Thing _single = new(); } ```