Quick - do you know the difference between the two following classes?

    class Test

    {

        static object o = new object();

    }

 

    class Test

    {

        static object o;

 

        static Test()

        {

            o = new object();

        }

    }

(example shamelessly stolen from here)
 
Says Jon:
The two classes are not, in fact, the same. They both have type initializers - and the two type initializers are the same. However, the first does not have a static constructor, whereas the second does. This means that the first class can be marked as beforefieldinit and have its type initializer invoked at any time before the first reference to a static field in it.
If you read that more than once (I know I did) - then get yourself over to this excellent explanation of what's going on.  Jon (couldn't find his last name) also has a number of articles about C#, Java, and some other things you may be interested in if your geek score is higher than mine.
 
Somebody get this guy a blog!
 
- G


Creative Commons License This work is licensed under a Creative Commons License.