Static Class vs Singleton Class in C# OOP

Cem Tuğanlı
6 min readMar 29, 2023

--

A static class is a class in which we can not create an instance or object of this class. Every member inside a static class must be static. Fields, properties, methods, even nested classes inside the outer class must be marked as static.

For example, if we have a company with products, and we use the same logo and company name for each product, this situation corresponds to that of a static class. Assume that we have software to exhibit our products. All company names and logos of these products must be the same. This means that all the products draw the logo data and company name data from the same source from the same variable. This means that this same variable is being shared across the software.

To be precise, static classes are intended to provide utility functions or serve as a single point of entry to access functionality, without requiring object instantiation. Consequently, they are unable to support non-static instance constructors, instance members, inheritance, or interface implementation. As a result, object-oriented programming functionalities such as inheritance, encapsulation, and polymorphism are not relevant to static classes. Nonetheless, static classes are able to support static constructors, static members, and extension methods. A static class’ methods, variables, and properties are shared inside the application and accessed by the using class name then dot and followed by the member name(invoking).

public static class StaticClass
{
//fields
public static int number;
private static string numberName;

//methods
public static void DoSomething()
{
// code to perform some action
}

//everything must be static
}

A static class does not follow the OOP rules, hence it is not a design pattern.

Static Classes:

  1. A static class can not be instanced.
  2. Static classes can not implement inheritance, which means they can not inherit other classes or be inherited by other classes other than the Object class. Static classes can not inherit from other static classes either. Static classes are sealed by default, which means they can not inherit other classes. It does not make sense for it to inherit from another class, as inheritance is used to create new objects of a derived class that inherit properties and methods from a base class. Static class cannot be instantiated, this is its similarity to abstract classes, however, a static class is not an abstract class by default, and a class can not be both static and abstract. Static classes do not allow inheritance but abstract classes do and makes inheritance a necessity. An abstract class is a class that can not be instantiated and must be inherited by a subclass, while a static class can not implement inheritance at all, since its purpose is solely to have data shared across all software, rather than shared via the child-inheritance-parent relationship..
  3. Static classes can not implement interfaces. By definition, interfaces are a set of behavior that a class can implement. Interfaces define a contract for an instance of a class. Interfaces are used to act on instances. Since you can not instantiate a static class, Interfaces can’t have static methods either. A class that implements an interface needs to implement them all as instance methods. Static classes can’t have instance methods either. Maybe if there could be static methods inside the interfaces, it would be possible to invoke them by the classical way of using static class and calling methods. (though after .NET 7 interface can have static methods.)
  4. Static classes do not support polymorphism. Static methods can not be overridden, so in a static class, none of the methods could be overridden. Static methods can not have virtual modifiers nor override modifiers since a static class can not inherit from any class including an abstract class.
  5. Static classes can not be passed by value to a method and can not be a data type for an argument if the method is either a static method or a non-static method. Because for all classes, passing them by value is like acting in a way for them to be instanced inside the argument parenthesis of the method(as if).
  6. A static class is generally initialized when it is first loaded for the first time and it may lead to potential classloader issues.
  7. We cannot implement the Dependency Injection design pattern using the Static class because the static class is not interface-driven.
  8. The static class does not have any Object pointer, so the scope is at the App Domain level.
  9. Provides thread safety by default, as only one instance of the static members exists in memory.
  10. Static classes(and so all static members) as objects are stored in the stack.
  11. Static classes cannot have instance constructors. They can only have private static parameterless constructors.
  12. Static classes are always loaded. They are loaded automatically by the common language runtime(CLR) when the program or namespace containing the class is loaded.
  13. Static members and static classes are not associated with any instance of the class.
  14. Static class can not dispose. because they are available to whole program until the end of the application’s lifetime.
  15. Static objects including static classes can not be cloned.

Singleton Classes:

A Singleton class is a class with only one instance. Singleton class will follow the OOP rules and it is a pure OOP implementation. A Singleton class can be passed as a parameter in a method. It is a design pattern that uses the OOP rules and this is the feature that makes the singleton class a design pattern.

public class SingletonClass
{
private static SingletonClass _instance;

private SingletonClass()
{
// private constructor prevents direct instantiation
}

public static SingletonClass Instance
{
get
{
if (_instance == null)
{
_instance = new SingletonClass();
}
return _instance;
}
}

public void DoSomething()
{
// code to perform some action
}
}
  1. A singleton class can only be instantiated inside the class itself.
  2. A singleton class’ property’s or field’s getters and setters are often validated to ensure that the same instance of the class is always accessed and the instance is not null.
  3. It allows non-static members in it.
  4. It can be passed as an argument in methods.
  5. It follows OOP rules, but can not be abstract, can implement interfaces, allow virtual and abstract methods, and support polymorphism. It can also inherit from other classes and allow inheritance, enabling it to be extended.
  6. It has a must-have private constructor that enables singleton class to only be instanced from the inside of the class and makes it not accessible from outside of the class and not possible to instance it.
  7. Singleton classes are not sealed by default, however, it is recommended to put sealed modifier to them.
  8. A Singleton class can be initialized by Lazy Loading or can be initialized by Eager Loaded.
  9. Singleton means a single object across the application lifecycle, so the scope is at the application level.
  10. Often used in scenarios where you need to maintain a single point of access to a shared resource, such as a database connection or a configuration manager
  11. Singleton class’ single instance is thread-safe too.
  12. Singleton Classes as objects are stored in Heap.
  13. Singleton classes can have instance constructors.
  14. Singleton classes are only associated with one and the same instance of the class.
  15. Singleton class can dispose because the class is not marked as “static”.
  16. Singleton classes can be cloned.

Important!

Objects that are created in the data segment are not garbage collected by Garbage Collector, so that’s why static members exist and can be used throughout the application’s lifespan. Singleton classes and static classes are stored in the data segment which is an area for global objects.

Use Static Classes instead of Singleton Classes:

When grouping together related functionalities but not requiring any internal state in any object, such as a Math class or Random class, static classes should be used. They have a ton of connected functions that can be accessed outside of the context of any particular object instance. A static class would typically be a wise choice if you have a collection of methods or constants that you want to access globally.

Use Singleton Classes instead of Static Classes:

A singleton is used when you do want an object with its own internal state, and you want to limit your system to exactly one instance of that object. This is useful if you have some kind of shared resources, such as a database, or an in-memory cache, as I stated in my 10th list bullet.

A Comment or a clap is much appreciated! Thank you for your time!

--

--