gpt4 book ai didi

C# 范围和 GC : Is outer reference-type object guaranteed to remain valid after assigned to inner object in nested "using" scope?

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:31 24 4
gpt4 key购买 nike

外部引用类型对象在分配给嵌套“使用”范围内的内部对象后是否保证保持有效?

A x;
{
using (A y = new A())
{
x = y;
}
}
x.foo(); // Is x rock-solid, guaranteed to be still valid here, or is this iffy? i.e. will the call to x.foo() bomb or work only "by chance"?

我很担心,因为我看到了 2 篇关于过早垃圾回收的文章,尽管它们的情况完全不同: http://www.curly-brace.com/favorite.html http://www.theserverside.net/tt/articles/showarticle.tss?id=GarbageCollection

最佳答案

有点。一旦离开 using block ,对象的 Dispose 方法将被调用,但对象引用仍然存在,因此您仍然可以调用 foo()。如果您在调用 Dispose 后尝试访问任何编写良好的对象,它都可能会抛出 ObjectDisposedException

关于C# 范围和 GC : Is outer reference-type object guaranteed to remain valid after assigned to inner object in nested "using" scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com