Click here to monitor SSC

Richard Mitchell

Project Manager - Red Gate Software
Developer and project manager at Red Gate Software Ltd. Now working solo on SQL Azure Backup

Which came first the static chicken or the static egg?

Published Wednesday, January 24, 2007 10:56 AM

Came across an interesting thing to do with static constructors in the course of the development of ANTS Profiler 3. We got around to talking about static constructors across multiple files and wondering what would happen if you attempted to create a circular reference of static initialisers. In the result you can see that the circular reference works exactly as you would expect. The static member variables being initialised to the default before any code got run so that wasn't interesting. However what if one of them is instantiated to a value and you refer to that value in the static method to instantiate another member variable (say for example opening a log file or something?) what would happen then?

So we come to the code:

using System;
namespace RedGate
{
  
partial class ChickenOrEgg
   {
        
static string egg = ValueOfChicken();
        
static string ValueOfEgg()
      {
           
return egg;
      }
     
static void Main(string[] args)
      {
        
Console.WriteLine("chicken={0} egg={1}", chicken, egg);
        
Console.WriteLine("Press any key...");
        
Console.ReadKey();
      }
   }
  
partial class ChickenOrEgg
   {
     
static string chicken = "Chicken"; // ValueOfEgg();
      static string ValueOfChicken()
     {
        
return chicken;
     }
   }
}

 

So what do we expect the output of this program to be? Well personally I would have thought...

 

chicken=Chicken egg=Chicken

The system noticing that the chicken member variable is set to be a static string so instantiating it first. But no the actual output of the program is...

chicken=Chicken egg=

So it's obvious that even though you would expect the static chicken string to be instantiated first as it has no dependencies that doesn't happen and if you look at the compiled code in Reflector you can see what the static constructor actually is.

static ChickenOrEgg()
{
      ChickenOrEgg.egg = ChickenOrEgg.ValueOfChicken();
      ChickenOrEgg.chicken =
"Chicken";
}


So here's a warning to all of you out there, don't assume anything about the order in which your static initialisers get called as it will only cause pain.

The answer to the initial question - the static chicken :)

Comments

No Comments
You need to sign in to comment on this blog
<January 2007>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...