-1

I am learning Singleton design pattern in C#, and I have written below code in two ways, and I want to know Which one is the right way to create a Singleton class:

public sealed class TranslationHelper
{
    // first way
    private static readonly TranslationHelper translationHelper = new TranslationHelper();
    // second way
    public static readonly TranslationHelper translationHelpers = new TranslationHelper(); // Direct call

    public static TranslationHelper GetTranslationHelper()
    {
        return translationHelper;
    }

    private TranslationHelper()
    {
    }
}

CALL:

TranslationHelper instance = TranslationHelper.GetTranslationHelper();
TranslationHelper ins = TranslationHelper.translationHelpers;

I am beginner so I am not sure if both methods are same. Please guide me.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • 6
    The best guide ever, is by @JonSkeet: http://csharpindepth.com/Articles/General/Singleton.aspx. You'll understand both are wrong once you read that – Camilo Terevinto Sep 04 '18 at 21:06
  • https://stackoverflow.com/q/2667024, https://stackoverflow.com/q/2902347 among many others – Ňɏssa Pøngjǣrdenlarp Sep 04 '18 at 21:06
  • The difference (aside from name) is between `public` and `private`. In general, you almost never want to have a `public` field - you should instead use a `public` [property](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties). As for the singleton question, other comments have resources for the design pattern. – Joe Sewell Sep 04 '18 at 21:09
  • @JoeSewell: Thank you for this input, do you mind adding one line of explanation too, please. – Unbreakable Sep 04 '18 at 21:11
  • @CamiloTerevinto now you got me curious. Is initializing in static constructor also wrong? static constructor is only called when the class is used and .net guarantees that it is only run once. (I.E. thread safe) – Steve Sep 04 '18 at 21:14
  • @Steve The static constructor runs after/before initializing static variables. There's a Q&A here I think answered by '@EricLippert that explains how hard it is to determine (if not impossible) which runs first though. Basically it is the same though, using the constructor just allows you to do things a normal method does – Camilo Terevinto Sep 04 '18 at 21:16
  • An class should not be responsible for the lifespan of the objects it provides, as this limits reuse of that class. Often, the singleton is used as a hack to give you a globally-scoped object and carries quite [a smell](https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons) with it. The best way to control object lifespans is using dependency injection. If you're not familiar with DI, it's well worth the ride. – spender Sep 04 '18 at 21:32

1 Answers1

5

If you are using .Net 4 or higher you can use the Lazy<T> type like this:

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}

By using this design pattern with a private constructor you are insuring that the class and it is only created at the time it is used. This is ensured because

  1. It can only be called by itself so it does not get instantiated ahead of time.
  2. The Lazy<T> keyword when used with the private static readonly lambda function now provides a visually clear way of lazily creating an instance of the class from within the function itself.
  3. The public Singleton Instance property provides a way to access that singleton from outside the class.
Dafrapster
  • 90
  • 5