Mummy, how are OutOfMemoryException instances born?

Reading the FXCop rules about weak identity locking targets made me wonder why these fellows had 'Weak Identity'
  1. System.OutOfMemoryException

  2. System.StackOVerflowException



That's because these exceptions only have on instance per CLR instance, as the CLR automatically allocates memory for them on startup, and thus doesn't run out of memory while trying to tell the user he ran out of memory.

The more you know! ™


There is no such thing as C# Static constructor : how to change readonly value without ref

There is no such thing as a C# static type constructor. Rather, C# does support static types initializers, which doesn't bear quite the same semantics.

Consider this fragment:

class Demo
{
  public static void Main(string[] args)
  {
    Console.Out.WriteLine("A.Val = {0}", A.Val);
    Console.Out.WriteLine("B.Val = {0}", B.Val);
  }
}

static class  A
{
  public static readonly int Val;
  static A(){ Val = B.Val + 1; }
}

static class B
{
  public static readonly int Val
  static B() { Val = A.Val + 1; }
}

The result will be...

A.Val = 2
B.Val = 1

If these were constructors, the CLR would throw an exception as the static constructor A is calling static B() which in turns references a field in A that hasn't yet been initialized, but the static class already exists.