5

Looking around the web, I've seen this simple pattern that implements a (thread-safe) singleton (in C#).

public sealed class MySingleton
{
    private static readonly MySingleton _instance = new MySingleton();

    private string[] _data = new string[10];

    // Private constructor, so no outsiders have access.
    private MySingleton()
    {
        // Initialize _data member here
    }

    // Static method to provide access to instance
    public static MySingleton Instance
    {
      get { return _instance; }
    }

    public string GetStringCount
    {
        return _data.Length;
    }

}

I understand that the _instance member needs to be declared as static because it is accessed from the static Instance() method.

But should other members be declared as static? By definition the singleton exists only once so the members can also only exist once (for the single instance), just as static variables exist only once.

Should I declare _data as static?

  • Will there be any functional difference?
  • Any performance difference?
  • Any other reason to prefer static or non-static?
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
njr101
  • 9,499
  • 7
  • 39
  • 56
  • 3
    The other members are related to the *object* which is created on your third line and named `_instance`. If you specify `_data` as static - it would be a class member, not an object member. So there is nothing to do with syntax or best practices, but with your intentions and aims. – zerkms May 06 '12 at 09:38
  • 1
    @zerkms clearly he knows that, but since it's a singleton the data would only exist once either way - there would be no real difference. – harold May 06 '12 at 09:44
  • @njr are you using the singleton as a workaround to let a static class implement interfaces (then I'd keep everything static as much as possible to match the original intent), or for some other reason? – harold May 06 '12 at 09:46
  • @harold: indeed, it is about semantics. But I wouldn't make the singleton some special and exceptional case, to not confuse further readers – zerkms May 06 '12 at 09:46
  • @harold: I'm using a singleton to be more flexible, since I may want to pass the object as a parameter in the future. To be honest, for the moment I could just use a static class. I was really just curious if there was any "real-world" difference between static and non-static members for singletons. – njr101 May 06 '12 at 09:50
  • "I'm using a singleton to be more flexible" --- just a side note: singletons don't make your code more flexible, but makes more coupled. – zerkms May 06 '12 at 09:53

2 Answers2

5

If you have a Singleton you have one instance of a class. The class members must not be static (except for the Instance property backing field). If you have more than one statics in your Singleton you have actually created not one Singleton but many Singletons. A general advice is to use the static keyword only when absolutely necessary.

It is cleaner to store your singelton data inside your singleton instance as non static class members.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • Thanks Alois. That's a clear answer. I can see what you're saying that it is cleaner to store instance data inside non-static members. I was curious if it would have any other side effects. – njr101 May 06 '12 at 09:52
  • Performance is not really an issue. There are no observable side effects except if you really try hard to show them. If all Singleton members are non static you will intialize all fields of your Singleton instance on first access. If you do hide inside your Singleton several resources it might be better to put them into differnt Singletons or you do initialize in your Singelton the rest later via Lazy members. – Alois Kraus May 06 '12 at 10:43
2

If you choose to use a singleton over a static class (see this and this), then I think it makes more sense to have instance members instead of static members.

Community
  • 1
  • 1
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Thanks for the links. dotnetperls is actually where I found this pattern in the first place :) – njr101 May 06 '12 at 09:56