-2

I have the following singleton implementation based on a video https://youtu.be/Q6HJpgdkAK8?si=k-9ksjHirLHq5Ne2 Some features of my own implementation are different, but i want to know whether my implementation is correct in general terms.

My Singleton class:

internal class CSingleton
{
    // Aquí guardamos la única instancia que va a existir.
    private static object instance;

    // Creamos el constructor privado.
    private CSingleton()
    {
        
    }

    public static object ObtenInstancia()
    {
        if (instance == null)
        {
            instance = new COctopus();
        }

        // Aseguramos que se crea una sola vez.
        return instance;
    }

    private class COctopus
    {
        private readonly string figure = "\t\t\t\t\t           °°°°°°°°\r\n\t\t\t\t\t\t°°°°°°°°°°°°°°°°\r\n\t\t\t\t\t   °°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t\t\t  °°°°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t   \t         °°°°°°°°°°°°°°°°°°°°°°°°°°°°\r\n                                         °°°°°°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t    \t          °°°°°°°  °°°°°°°  °°°°°°°         \r\n\t\t\t  °\t             °°°°°°°°°°°°°°°°°°°°     \t   \r\n\t\t\t °°\t    °         °°°°°°°°°°°°°°°°       °     \r\n\t\t\t °°°\t   °°\t\t°°°°°°°°°°°°°        °°     °\r\n\t\t\t  °°°\t  °°°            °°°°°°°°°°         °°°      °°\r\n\t\t\t   °°°° \t          °°°°°°°°                   °°°\r\n\t\t\t    °°°°°\t            °°°°°\t\t     °°°\r\n\t\t\t     °°°°°\t                                    °°°°\r\n\t\t\t      °°°°°      °°°°\t     -\t   -   °°°°        °°°°°\r\n\t\t\t        °°°\t °°°       /   \\  /  \\   °°°\t   °°°°\r\n\t\t\t\t\t °°°\t  |     \\/    |   °°°       °°°\r\n\t\t\t\t\t  °°\t   \\         /     °°\r\n\t\t\t\t\t   °°       \\       /      °°\r\n\t\t\t\t\t    °        \\     /       °\r\n\t\t\t\t\t\t      \\   /\t\r\n\t\t\t\t\t\t       \\_/\t";

        public override string ToString()
        {
            return String.Format(figure);
        }
    }
} 
7
  • How do you expect to utilize it? var x = ...? You don't need extra wrapper CSingleton, which holds private class COctopus, just make COctopus' constructor private. Commented Oct 26, 2024 at 23:32
  • I expect to utilize it by accesing the method and keep the return value whitin a object variable. Commented Oct 26, 2024 at 23:40
  • 1
    Everything you need to know about singletons - csharpindepth.com/Articles/Singleton Commented Oct 27, 2024 at 1:47
  • 1
    Define "correct". Also, nowadays, you typically use dependency injection and define lifetime by that. Commented Oct 27, 2024 at 5:23
  • If multi-threading is a concern, your code has a race condition and would thus be not correct. Commented Oct 27, 2024 at 7:53

2 Answers 2

1

Your actual class which does the job can hold singleton instance, you dont need wrapper class here.

Just do:

internal class COctopus
{
    private COctopus() { }

    public static COctopus Instance { get; } = new();

    public override string ToString()
    {
        // Your stuff here
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I´m making sure that the COctopus class is only accesible by accessing Singleton class. I don´t want COctopus class to be accesed by other way. Is my idea correct? Thanks for your help, it really helped me!!
you also would need the class to be sealed
-1

It's better to use double-check while creating an instance.

internal class COctopus
{
    private static volatile COctopus _instance;
    private static readonly object Lock = new object();

    private readonly string figure = "\t\t\t\t\t           °°°°°°°°\r\n\t\t\t\t\t\t°°°°°°°°°°°°°°°°\r\n\t\t\t\t\t   °°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t\t\t  °°°°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t   \t         °°°°°°°°°°°°°°°°°°°°°°°°°°°°\r\n                                         °°°°°°°°°°°°°°°°°°°°°°°°°°°°\r\n\t\t\t    \t          °°°°°°°  °°°°°°°  °°°°°°°         \r\n\t\t\t  °\t             °°°°°°°°°°°°°°°°°°°°     \t   \r\n\t\t\t °°\t    °         °°°°°°°°°°°°°°°°       °     \r\n\t\t\t °°°\t   °°\t\t°°°°°°°°°°°°°        °°     °\r\n\t\t\t  °°°\t  °°°            °°°°°°°°°°         °°°      °°\r\n\t\t\t   °°°° \t          °°°°°°°°                   °°°\r\n\t\t\t    °°°°°\t            °°°°°\t\t     °°°\r\n\t\t\t     °°°°°\t                                    °°°°\r\n\t\t\t      °°°°°      °°°°\t     -\t   -   °°°°        °°°°°\r\n\t\t\t        °°°\t °°°       /   \\  /  \\   °°°\t   °°°°\r\n\t\t\t\t\t °°°\t  |     \\/    |   °°°       °°°\r\n\t\t\t\t\t  °°\t   \\         /     °°\r\n\t\t\t\t\t   °°       \\       /      °°\r\n\t\t\t\t\t    °        \\     /       °\r\n\t\t\t\t\t\t      \\   /\t\r\n\t\t\t\t\t\t       \\_/\t";

     private COctopus()
     {
        
     }

     public static COctopus ObtenInstancia
     {
       get
       {
            if (_instance == null)
            {
                lock (Lock)
                {
                    if (_instance == null)
                        _instance = new COctopus();
                }
            }
            return _instance;
        }
     }

    public override string ToString()
    {
       return String.Format(figure);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.