Deterministic Finalization IV - Benefits, part II

Long ago, I wrote a post on the first part of DF benefits. Now, I'm finally getting back to it. My apologies about the laxness in posting. Blame it on my Cards losing to the Sox. And on being really busy with testpasses and bug bounces for a while. We're finally settling down a little bit, so that gives me time to pull out the keyboard and do some more blogging.

Last time, we discussed ref types on the stack, and the benefits they provide. But, if you can put ref types on the stack, where else can you put them? Try this one on for size:

using namespace System;

ref struct A{ ~A(){ Console::WriteLine("~A()"); } };
ref struct B{
A a1;
A a2;
};

int main(){
B b;
}

Compile and run that, and you'll see ~A() output twice. Now, you can have ref types as class members, and we call the destructors for you automatically. Dig this, they can even be static! The same rules as native C++ apply all over the place. If I want to call a constructor with arguments, I can do it inside the ctor initializer list. And object unwinding works as well.

Okay, so maybe I didn't need to split this into a separate posting. I could have bundled it, and divluged this little nugget of coolness weeks ago. In any case, next time, I'll wrap up this whole DF business with a discussion on how our DF model works with the CLR Dispose pattern. (See part 1, or some CLR persons' blog for more on the CLR Dispose pattern.) After that, I'll probably get into copy ctors on ref types, or maybe my new favorite subject: hidebysig.