简体   繁体   English

从列表中删除项目的正确方法是什么 <WeakReference<T> &gt;?

[英]What is the proper way to remove items from a List<WeakReference<T>>?

Is the following loop approach, using TryGetTarget then compare the proper way? 以下是使用TryGetTarget的循环方法,然后比较正确的方法吗?

void Remove<T>( List<WeakReference<T>> list, T toRemove ) where T : class {
    for(var i=0; i<list.Count; ++i) {
        if(list[i].TryGetTarget(out var el) && el==toRemove) {
            list.RemoveAt(i);
            break;
        }
    }
}

Is there a more elegant or suggested way to do this? 有没有更优雅或建议的方法来做到这一点?

您可以将其缩短为:

list.RemoveAll(item => item.TryGetTarget(out var el) && el == toRemove);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM