Take a look at this little program skeleton:
class Foo
{
private int x;
private int y;
public Foo(int x, int y)
{
this.x = x;
this.y = y;
SideEffects.Alpha(); // Notice: does not use "this"
}
~Foo()
{
SideEffects.Charlie();
}
}
static class SideEffects
{
public static void Alpha() { ... }
public static void Bravo() { ... }
public static void Charlie() { ... }
public static void M()
{
Foo foo = new Foo(1, 2);
Bravo();
}
}
Let's suppose we have three side effects: Alpha, Bravo and Charlie. What precisely they are is not important.
The question is: what do we know about the order in which Alpha, Bravo and Charlie execute when a call to M() occurs?
First off, clearly Alpha must happen before Bravo. The C# compiler and the jit compiler are not permitted to make any optimization that would cause the side effects of a single-threaded program to appear out of order. The construction of foo must be complete before control is passed to Bravo, which means that Alpha has already run.
Read more: Fabulous Adventures In Coding
QR: