You made a mistake - A.this() sets the global b, while "B b=new B()"
creates the local b.. When you execute b.dg(), it's not the global b, so
it doesn't have the delegate set, hence AV.. In fact, I'd say that it's
A.this() that produces AV, because the global b is null...
xs0

You made a mistake - A.this() sets the global b, while "B b=new B()"
creates the local b.. When you execute b.dg(), it's not the global b, so
it doesn't have the delegate set, hence AV.. In fact, I'd say that it's
A.this() that produces AV, because the global b is null...
xs0

My guess is that the problem is that the delegate you're creating is a
stack delegate, and the stack frame is no longer valid. Remembery that
the line
foo();
in the delegate literal is really
this.foo();
and that, since this is a stack delegate, the compiler is gettting
'this' from a function argument which was on the stack; thus, the
statement really becomes
stack_frame.this.foo();
where stack_frame is the pointer which is saved in the delegate. This
pointer is created when the delegate literal is created, and is invalid
from the moment that the stack frame (the constructor) exits.
This code should work fine if you change A's constructor to this:
this() { b.dg = &this.foo; }

My guess is that the problem is that the delegate you're creating is a
stack delegate, and the stack frame is no longer valid. Remembery that
the line
foo();
in the delegate literal is really
this.foo();
and that, since this is a stack delegate, the compiler is gettting
'this' from a function argument which was on the stack; thus, the
statement really becomes
stack_frame.this.foo();
where stack_frame is the pointer which is saved in the delegate. This
pointer is created when the delegate literal is created, and is invalid
from the moment that the stack frame (the constructor) exits.
This code should work fine if you change A's constructor to this:
this() { b.dg = &this.foo; }