[.net] Static Member Initialization

Started by
20 comments, last by phresnel 14 years, 6 months ago
I guess I am getting in the discussion a little late.

By reviewing your code, I'd suggest you use in-property initialization, or, even better, a singleton.

By in-property initialization, I mean:
interface IDictionaryContainer{  Dictionary<string, int> Container { get; }}class DictionaryContainer : IDictionaryContainer{  private static Dictionary<string, int> singletonContainer;  public Dictionary<string, int> Container  {    get    {      if (singletonContainer == null)      {        singletonContainer = new Dictionary<string, int>();        singletonContainer["ten"] = 10;        singletonContainer["twenty"] = 20;        singletonContainer["thirty"] = 30;      }      return singletonContainer;    }  }}


I am a developer, but at work I happen to do some support, with a lot worse appreciation for advise or cooperation. So, don't get angry with him/her. He/she probably doesn't care at all, so don't waste your time being angry.

To the thread poster: instead of worrying whether and when your supposed Init method is called, you can do in-property initialization. You get the benefit of always having your property initialized and a little something called late-binding (a kind of, the only stuff that ever gets initialized is the one you use). The drawback is that first operations are going to be slower.
Advertisement
Quote:Original post by Sambori
Calm down rage!


Quote:Military Coding Handbook, p. 853
[0] You shall not code under heavy artillery fire.
[1] If [0] is not applicable, stay calm and rational. Minefield code is doomed, and so is everyone running your code.

This topic is closed to new replies.

Advertisement