gpt4 book ai didi

c# - 为什么 WeakReference 不是结构体?

转载 作者:太空狗 更新时间:2023-10-29 20:39:02 24 4
gpt4 key购买 nike

正如这篇文章所说:http://www.philosophicalgeek.com/2014/08/14/prefer-weakreferencet-to-weakreference/

If you are using WeakReference at all, it likely means you are somewhat memory conscious . In this case, allocating new WeakReference objects will contribute extra, unnecessary memory pressure.

所以在这种情况下,将它变成一个结构不是更有意义吗?并且在我们使用它时是不可变的,并摆脱 SetTarget()。

最佳答案

WeakReference WeakReference<T> 类都有终结器,wouldn't be possible if they were structs .

如果确实有必要,您可以使用 weak 创建自己的自定义弱引用结构。品种 GCHandle . (我相信,虽然我不确定,WeakReferenceWeakReference<T> 在内部使用 GCHandle,尽管它们的终结器确保一切都被正确清理。您的自定义结构需要处理它自己的清理-不求助于终结器。)

关于c# - 为什么 WeakReference 不是结构体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31320061/

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