X hits on this document





270 / 396

Chapter ‎18   Unsafe code

class B { static B() { Console.WriteLine("Init B"); } public static void F() { Console.WriteLine("B.F"); } }

must produce the output:

Init A A.F Init B B.F

because the execution of A’s static constructor is triggered by the call to A.F, and the execution of B’s static constructor is triggered by the call to B.F.

It is possible to construct circular dependencies that allow static fields with variable initializers to be observed in their default value state.

The example

using System;

class A { public static int X;

static A() { X = B.Y + 1; } }

class B { public static int Y = A.X + 1;

static B() {}

static void Main() { Console.WriteLine("X = {0}, Y = {1}", A.X, B.Y); } }

produces the output

X = 1, Y = 2

To execute the Main method, the system first runs the initializer for B.Y, prior to class B’s static constructor. Y’s initializer causes A’s static constructor to be run because the value of A.X is referenced. The static constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, which is zero. A.X is thus initialized to 1. The process of running A’s static field initializers and static constructor then completes, returning to the calculation of the initial value of Y, the result of which becomes 2.

10.12 Destructors

A destructor is a member that implements the actions required to destruct an instance of a class. A destructor is declared using a destructor-declaration:

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.257

Document info
Document views1453
Page views1453
Page last viewedTue Jan 24 05:04:05 UTC 2017