Comments on: When to Use Static Classes in C# https://code-maze.com/static-classes-csharp/ Learn. Code. Succeed. Wed, 08 Nov 2023 11:23:33 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: naresh https://code-maze.com/static-classes-csharp/#comment-6195 Tue, 02 Aug 2022 11:48:47 +0000 https://drafts.code-maze.com/?p=61445#comment-6195 thanks

]]>
By: Vladimir Pecanac https://code-maze.com/static-classes-csharp/#comment-4865 Mon, 03 Jan 2022 10:02:29 +0000 https://drafts.code-maze.com/?p=61445#comment-4865 In reply to Flo.

That makes sense and it’s true. What we meant is that static classes cannot be instantiated by a user.

Thank you for your input, we’ll clarify that in the text.

]]>
By: Vladimir Pecanac https://code-maze.com/static-classes-csharp/#comment-4864 Mon, 03 Jan 2022 09:58:20 +0000 https://drafts.code-maze.com/?p=61445#comment-4864 In reply to Joe.

Hey Joe, that’s right. We should have made it clear that we’re talking about instance constructors, not the static ones. Static classes can indeed have one static constructor. We’ve made some changes to make it more clear.

]]>
By: Flo https://code-maze.com/static-classes-csharp/#comment-4859 Mon, 03 Jan 2022 06:28:57 +0000 https://drafts.code-maze.com/?p=61445#comment-4859 A static class is not a class that can not be instatianted. A static class is a class that can have one instance only through the entire lifecycle of the app. Major difference in these concepts.

To make sure static classes have a single instance, microsoft designed them to support a parameterless static constructor which is also private, therefore, the static classes can not be instantiated by code; they are instantiated automatically by the framework.

To summerize using causality:
The cause of a static class is to have a sinlge instance, therefore, the effect is it can not be instantiated by code; it is instantiated automatically by runtime

]]>
By: Joe https://code-maze.com/static-classes-csharp/#comment-4857 Mon, 03 Jan 2022 01:41:59 +0000 https://drafts.code-maze.com/?p=61445#comment-4857 Why does this claim static classes cannot have a constructor? They definitely can, a static constructor..

]]>
By: Dennis Aries https://code-maze.com/static-classes-csharp/#comment-4848 Sat, 01 Jan 2022 07:34:37 +0000 https://drafts.code-maze.com/?p=61445#comment-4848 In reply to Roy Malka.

In this db-example, a singleton-class would be preferred over a static class.

A big downside of static classes is that you cannot mock them for testing.

Static classes are usually used for defining extensions on other classes and holding static values like string-constants that are not user-oriented (those should be in the resources where it is easier to provide translations).

]]>
By: Roy Malka https://code-maze.com/static-classes-csharp/#comment-4845 Sat, 01 Jan 2022 05:45:42 +0000 https://drafts.code-maze.com/?p=61445#comment-4845 Another advantage is that it is single instance and you can use the same instance through the entire application, for example a DB accessor that you want to lock is methods.
** of course it’s better to use DI mechanism for this things.

]]>