C# parameters by reference and .net garbage collection

后端 未结 3 2036
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 14:57

I have been trying to figure out the intricacies of the .NET garbage collection system and I have a question related to C# reference parameters. If I understand correctly, v

3条回答
  •  Happy的楠姐
    2020-12-17 15:28

    No, you don't need to worry about it. Basically the calling method (DoIt) has a "live" reference to the instance of Test, which will prevent it from being garbage collected. I'm not sure whether it can be compacted - but I suspect it can, with the GC able to spot which variable references are part of objects being moved.

    In other words - don't worry. Whether it can be compacted or not, it shouldn't cause you a problem.

提交回复
热议问题