Search code examples
blazorblazor-client-side

Use singleton into a class with only static methods and no constructor?


I have a classes with only static methods, and no constructors in a library, and I need to access properties from a singleton object which is shared across the .razor files in a WebASM project.

  • How use singleton into a class with only static methods and no constructor ?

Solution

  • How use singleton into a class with only static methods and no constructor ?

    That is a mismatch. The Singleton is managed by the DI and is created later. Potentially it could be recreated or cleared later on.

    So treat them the same: make both objects Singletons (preferred) or make both static.

    In general you should avoid the temptation of static, especially for state keeping.