Suppose I have three objects: 'a', 'b' and 'c'. Object 'a' and 'c' are long-lived, statically referenced service singletons. Object 'b' is short-lived, i.e. no static references keep it alive.
Now suppose object 'a' creates an instance of object 'b' in the scope of one of its methods, e.g.
B b = new B();
Further suppose that the class B looks something like this:
public B ()
{
C.ActionList.Add ( SomeMethod );
}
void SomeMethod ()
{
...
}
Now, how long does object 'b' live? My presumption is that it lives beyond the scope of the method that called its constructor; specifically, for as long as its method is still in the 'ActionList' of object 'c'.
Is that correct? If not, and it gets garbage collected, what happens when 'c' runs all the methods in its 'ActionList'?
Bonus question: What if the method on 'b' is not named, but anonymous and written in the constructor as follows:
public B ()
{
C.ActionList.Add ( () => {
...
} );
}