Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is an Example of a Singleton class with Eager Loading Pattern and a multithreaded environment is created by using Parallel.Invoke func. (Console App)

The Question is : Why only object is being created here ? Kind of confused.

Try this code in a console app and just presss that f5.


using RydoZone;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RydoZone
{
    public sealed class Rydo
    {
        private static int counter = 0;
        //private static Rydo instance = null;

        private static Rydo instance = new Rydo();

        //private static object obj = new Object();
        public static Rydo GetInstance
        {
            get
            {
                return instance;
            }
        }
        private Rydo()
        {
            counter++;
            Console.WriteLine("Counter Value is" + counter.ToString());
        }
        private void PrintDetails()
        {
            Console.WriteLine("I am Print Method of Singleton Class");
        }

        public void ok()
        {
            Console.WriteLine("I am Print Method of Singleton Class");
        }
    }
}

public class Mained
{
    static void Main()
    {
        Parallel.Invoke(() => RydoCallfromHamas(), () => RydoCallfromUS()); // Dummy MultiThreaded Environment
        Console.ReadKey();
    }

    private static void RydoCallfromHamas()
    {
        Rydo r = Rydo.GetInstance;
        Console.WriteLine("Hamas got the Rydo Instance");
    }

    private static void RydoCallfromUS()
    {
        Rydo r = Rydo.GetInstance;
        Console.WriteLine("US got the Rydo Instance");
    }
}


What I have tried:

Googling and nothing much. Please help. That is it.
Posted
Updated 31-May-21 9:07am
Comments
BillWoodruff 1-Jun-21 4:09am    
What is your goal here ? What doesn't work, or, causes an error ?
vaibhav1800 1-Jun-21 7:29am    
My Goal here is to have two instances when the instance is defined with static keyword.
If instance was of static readonly type then only one time instance creation would have made sense. But its just static. I beleive that when its static, then two instances should be created.
vaibhav1800 1-Jun-21 7:29am    
So If I change private static Rydo instance = new Rydo();
to private readonly static Rydo instance = new Rydo();
then single object creation makes sense

1 solution

Quote:
The Question is : Why only object is being created here ?
Because that is what the Singleton pattern is all about: a single instance is created and no others can be.
Singleton pattern - Wikipedia[^]
 
Share this answer
 
Comments
vaibhav1800 1-Jun-21 6:40am    
That I get it. But this piece of code : private static Rydo instance = new Rydo();

Why is this getting executed only once ? I mean we are parallely invoking instances, one from RydoCallfromHamas and RydocallfromUS. So that piece of code should be executed twice, once when it is called from Hamas and next when its from US. How is it behaving to execute only once and hence creating only one instance. ?
OriginalGriff 1-Jun-21 7:56am    
Because it's a static variable - so there is only ever one of it in the whole application, and it's constructor is called once only: the first time it is used in your app, the static constructor is called and the one and only instance is created and loaded into it.
It doesn't matter how many threads access it*, a static constructor is only ever called once per execution of the app.



* Well ... strictly speaking it does: unless it's properties and fields are coded to be thread safe it really does matter! But ... "lies to children" ... :laugh:
vaibhav1800 2-Jun-21 5:05am    
Haha. I see. I agree with you. Then Do I really need to use readonly keyword here ? as static standalone does the job of creating a single instance.

private readonly static Rydo instance = new Rydo();
OriginalGriff 2-Jun-21 5:45am    
You can, but since it's private, you probably don't need to as only the class itself can change it anyway.
vaibhav1800 1-Jun-21 7:27am    
Its just static and not readonly. If it was readonly then only one time instance creation would make sense. But its just static.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900