Search code examples
c#classstaticstatic-variablesstatic-constructor

When does a static member of a class get accessed before the static constructor of the same class


I have a public class and have two static members , I want to set the value of these static members using a static constructor . But the member getting accessed from .aspx page even before the static constructor initializes it .

Any input on how to prevent this and get the constructor to be hit before always .

Adding a small code reference for this :

 public class test
    {
        public string var1 ;
        public string Description;
        public string var2;
        public string var3;

        public static List<Feature> MasterFeatureList = new List<Feature>();
    
        static test()
        {
            try
            {
                if (MasterFeatureList.Count() == 0)
                {
                    using (IM5Context context = new M5Context())
                    {
                        MasterFeatureList = 
    new 

 FeatureRepository(context).GetAll().Where(x => x.Enabled == true).ToList();
                    }
                }

               
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }

        public static Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
            {
            {
                Feature.Values.xyz,
                new test { var1 = MasterFeatureList.Find(x=>x.Id==(int) Feature.Values.xyz).Name, Description = "", var2 = "xyz", var3 = "xyz" }
            },
// i have multiple other feature to be initilaized 

The above code has a static constructor which has a static member and I am using that in the static dictionary to initialize the values .But the dictionry gets accessed before the static constructor initializes it .


Solution

  • In order to handle the above case instead of using the constructor to initialize the static master list, i have added a explicit call to static function that will initialize the static masterlist and then can check if the masterlist has a value use that

    public static string SetName(int featureId)
    {
        using (IM5Context context = new M5Context())
        {
            if (MasterFeatureList.Count() == 0)
            {
                MasterFeatureList = new xyzRepository(context).GetAll().Where(x => x.Enabled == true).ToList();                  
            }
    
            if (MasterFeatureList.Any(x => x.Id == featureId))
            {
                return MasterFeatureList.Find(x => x.Id == featureId).Name;
            }
            else
            {
                 return  new FeatureRepository(context).GetById(featureId).Name;
            }                
        }
    }
    
    public static readonly Dictionary<Feature.Values, test> Features = new Dictionary<Feature.Values, test>()
    {
        {
            Feature.Values.xyz,
         new test { ServiceName = SetName((int)Feature.Values.xyz), Description = "", KbUrl = "xyz", TemplateAppendix = "xyz" }
        },