Let's say I have the following code:
public class Foo
{
private int x;
private int y;
public Bar CreateBar()
{
return new Bar(x, () => y);
}
}
[Serializable]
public class Bar
{
private int a;
private Func<int> b;
public Bar(int a, Func<int> b)
{
this.a = a;
this.b = b;
}
}
What happens with the scope of the objects and values in this scenario? Since x is a value type, it is passed to Bar by value, and therefore, nothing needs to happen to its scope. But what happens to y? The value for y needs to stick around to be returned when b is actually evaluated. Is all of Foo kept around to evaluate y at a later time? I can only assume that Foo is not GC'ed.
Now let's say that we serialize Bar to disk, then deserialize it later. What has actually been serialized? Did it serialze Foo as well? What magic has gone on so that b can be evaluated after Bar has been deserialized? Can you explain what is happening in the IL?