我真的必须将其存储在临时局部变量中,而不使用它,并在方法结束时让垃圾收集器收集它吗?看起来很傻..
功能:
public static class ConcurrentDictionaryEx {
public static bool TryRemove<TKey, TValue>(
this ConcurrentDictionary<TKey, TValue> self, TKey key) {
TValue ignored;
return self.TryRemove(key, out ignored);
}
}
更新:或者,正如评论中提到的Dialecticus,只需使用Remove
IDictionary<TKey, TValue>
的引用,如果您想避免强制转换
ConcurrentDictionary<TKey, TValue>
引用,这将导致您返回创建扩展方法:
public static class ConcurrentDictionaryEx {
public static bool Remove<TKey, TValue>(
this ConcurrentDictionary<TKey, TValue> self, TKey key) {
return ((IDictionary<TKey, TValue>)self).Remove(key);
}
}
IDictionary.Remove(key)
。它处于阴影状态,因此您必须显式调用它。示例:
var dict = new ConcurrentDictionary<string, string>();
dict.AddOrUpdate("mykey", (val) => "test", (val1, val2) => "test");
((IDictionary)dict).Remove("mykey");
TryRemove(key, out value)
方法可以为您提供操作是否发生任何更改的反馈。使用最适合您需求的一种。
ConcurrentDictionary
中删除的项目执行某些操作。例如,假设您有一个
ConcurrentDictionary<int, MyDisposable>
,其中
MyDisposable
实现了
IDisposable
。对于从字典中删除的项目,
ConcurrentDictionary.TryRemove(...)
不会调用
.Dispose();
。在下面的代码中,
.Dispose();
调用成功,因为
MyDisposable
尚未被释放。
void Main()
{
var dict = new ConcurrentDictionary<int, MyDisposable>();
dict.TryAdd(1, new MyDisposable());
dict.TryRemove(1, out var d);
d.Dispose();
}
public class MyDisposable : IDisposable {
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~MyDisposable()
// {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
out
任何参数的重载:
public bool TryRemove(KeyValuePair<TKey, TValue> item)