Lazy loading vs Eager loading in Singleton Design Pattern in C#

Lazy loading vs Eager loading in Singleton Design Pattern in C#

In this article, We are going to discuss the Lazy vs Eager loading in Singleton Design Pattern to Achieve Thread Safety in C# with a real-time example. Please read our previous content/article before proceeding to this article where we discussed the Thread Safe Singleton Design Pattern in C# with real-time examples. As part of this content, we are going to discuss the following main points.

  1. What is Non-Lazy or Eager Initialization 
  2. Understanding The C# Lazy Keyword
  3. Lazy or Deferred Loading Initialization?
  4. How to make the singleton instance thread-safe using both Eager loading and Lazy loading with Examples?

What is Non-Lazy or Eager Initialization 

We can enhance the previous singleton design patterns article to a non-lazy loading pattern and we can call it an eager loading pattern.
It is simple and nothing but initialized the required object before it is accessed. which means we are going to create the object and keep it ready and use it when we need it.

Note: CLR takes care of the thread-safety. 

Below are the steps to achieve the eager loading.

Step 1:  Change the syntax in the Singleton class to below which means create a new object.

Step 2: change to readonly property.

Step 3: Remove double-check locking, single check locking, and object initialization


From : private static Singleton_Design_Pattern singletonInstance = null;
To :   private static readonly Singleton_Design_Pattern singletonInstance = new Singleton_Design_Pattern();

Previous Singleton.cs class file looks like below code

using System;

using System.Threading.Tasks;

namespace ConsolePractice

{

   public sealed class Singleton_Design_Pattern

    {

        private static int count =0;

        private static Singleton_Design_Pattern singletonInstance = null;

        private static readonly object objctlock = new object();

        public static Singleton_Design_Pattern GetNewInstance

        {

            get {

                lock (objctlock)

                {

                    if (singletonInstance == null)

                    {

                        singletonInstance = new Singleton_Design_Pattern();

                    }

 

                }

              

                return singletonInstance;

            }

        }

 

        private Singleton_Design_Pattern()

        {

            count++;

            Console.WriteLine("object "+count);

        }

        public void getEmployee(string message)

        {

            Console.WriteLine(message);

        }

    }

}


New Singleton Class looks like below code.


using System;

using System.Threading.Tasks;

 

namespace ConsolePractice

{

   public sealed class Singleton_Design_Pattern

    {

        private static int count =0;

        private static readonly Singleton_Design_Pattern singletonInstance =                                           new   Singleton_Design_Pattern();

               public static Singleton_Design_Pattern GetNewInstance

        {

            get

            {

 

                return singletonInstance;

            }

        }

 

        private Singleton_Design_Pattern()

        {

            count++;

            Console.WriteLine("object "+count);

        }

        public void getEmployee(string message)

        {

            Console.WriteLine(message);

        }

    }

}



What are Lazy or Deferred Loading Initialization and  Lazy keyword?

The lazy keyword was available in dot net framework 4.0 and above version. it provides support for lazy initialization. in order to make a property lazy, we need to pass the object type to this lazy keyword which is being lazily initialized.

to achieve this use lambda expression as a delegate.

Below is the code for lazy loading


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsolePractice

{

   public sealed class Singleton_Design_Pattern

    {

        private static int count =0;

        private static readonly Lazy<Singleton_Design_Pattern>  singletonInstance =

            new Lazy<Singleton_Design_Pattern>(()=>new Singleton_Design_Pattern());

               public static Singleton_Design_Pattern GetNewInstance

        {

            get

            {

 

                return singletonInstance.Value;

            }

        }

 

        private Singleton_Design_Pattern()

        {

            count++;

            Console.WriteLine("object "+count);

        }

        public void getEmployee(string message)

        {

            Console.WriteLine(message);

        }

    }

}

 
















Previous Post Next Post