Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
singleton
static
class

C# static vs. singleton class

Educative Answers Team
svg viewer

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 new keyword. To access the members and methods of a static class, write classname followed 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 static keyword; 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 square
  static class RectangleOrSquare
  {

    // Method to evaluate whether it is a
    // Rectangle or a Square 
    public 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 method
    static 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 square
  class RectangleOrSquare
  {
    // Instantiating singleton class
    static RectangleOrSquare Instance = new RectangleOrSquare();

    // Private constructor
    private RectangleOrSquare(){}

    // Method to retrive the singleton instance
    public static RectangleOrSquare getInstance
    {
      get
      {
        return Instance;
      }
    }

    // Method to evaluate whether it is a
    // Rectangle or a Square
    public 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 1 
      RectangleOrSquare.getInstance.Evaluate(5, 9);
      RectangleOrSquare.getInstance.Evaluate(6, 6);

      // Method 2
      RectangleOrSquare thisInstance  = RectangleOrSquare.getInstance;

      thisInstance.Evaluate(5, 9);
      thisInstance.Evaluate(6, 6);
    } 
  }
}

RELATED TAGS

c#
singleton
static
class
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring