C# static vs. singleton class
Overview
Static is a keyword, whereas Singleton is a design implementation. A programmer has the liberty to make the class either singleton or static, depending on the needs of the program. Each type offers key features, which is why you need to know the differences between them.
Static
To create a static class, we use the static keyword.
static class <classname> {}
There are some specific features of a static class:
- A static class cannot be instantiated using the
newkeyword. To access the members and methods of a static class, writeclassnamefollowed by a period and then the name of the member or method.
<classname>.<member/method>
- All the members and methods inside a static class are to be declared with a
statickeyword; otherwise, a compile-time error will be generated. - Static members are put on the high-frequency heap area of the memory because these members run for the lifetime of the program. They are not to be destroyed by the Garbage Collector.
- Static classes cannot inherit; they are sealed.
using System;namespace StaticvsSingleton{// Class takes length and width as inputs// and tells if it is a rectangle or a squarestatic class RectangleOrSquare{// Method to evaluate whether it is a// Rectangle or a Squarepublic static void Evaluate(int length, int width){if (length == width){Console.WriteLine("It is a square");}else{Console.WriteLine("It is a rectangle");}}}class MyProgram{// Main methodstatic public void Main(){RectangleOrSquare.Evaluate(5, 9);RectangleOrSquare.Evaluate(6, 6);}}}
Singleton
To create a singleton class, you need to instantiate an instance inside the class and create a method to return the same instance every time the class object is called. Since the instance remains the same throughout the program, we use the static keyword.
static <classname> <instance name> = new <classname>()
public static getInstance
{
get {return <instance name> }
}
There are some specific features of a singleton class:
- There is only one instance of the class; so, whenever the object is accessed, the same instance is returned to the user.
- Singleton objects can be passed as a reference to methods, unlike a static object.
- Singleton classes have a private constructor, this ensures that they are not instantiated from anywhere outside the class.
- Singleton objects are allocated memory on the heap.
- Singleton classes can inherit from other classes​.
using System;namespace StaticvsSingleton{// Class takes length and width as inputs// and tells if it is a rectangle or a squareclass RectangleOrSquare{// Instantiating singleton classstatic RectangleOrSquare Instance = new RectangleOrSquare();// Private constructorprivate RectangleOrSquare(){}// Method to retrive the singleton instancepublic static RectangleOrSquare getInstance{get{return Instance;}}// Method to evaluate whether it is a// Rectangle or a Squarepublic void Evaluate(int length, int width){if (length == width){Console.WriteLine("It is a square");}else{Console.WriteLine("It is a rectangle");}}}class MyProgram{static public void Main(){// You can access singleton class in two ways// Method 1RectangleOrSquare.getInstance.Evaluate(5, 9);RectangleOrSquare.getInstance.Evaluate(6, 6);// Method 2RectangleOrSquare thisInstance = RectangleOrSquare.getInstance;thisInstance.Evaluate(5, 9);thisInstance.Evaluate(6, 6);}}}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved