Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么ConcurrentDictionary.TryRemove需要第二个out参数?_C#_Dictionary - Fatal编程技术网

C# 为什么ConcurrentDictionary.TryRemove需要第二个out参数?

C# 为什么ConcurrentDictionary.TryRemove需要第二个out参数?,c#,dictionary,C#,Dictionary,我只想删除一个值。。之后我不需要使用变量。为什么不在不需要第二个参数的地方包含重载 我真的需要将它存储在一个临时局部变量中,而不是使用它,并在方法结束时让垃圾收集器收集它吗?似乎有点傻 函数:您可以准确创建所需的方法: public static class ConcurrentDictionaryEx { public static bool TryRemove<TKey, TValue>( this ConcurrentDictionary<TKey, TVal

我只想删除一个值。。之后我不需要使用变量。为什么不在不需要第二个参数的地方包含重载

我真的需要将它存储在一个临时局部变量中,而不是使用它,并在方法结束时让垃圾收集器收集它吗?似乎有点傻


函数:

您可以准确创建所需的方法:

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);
  }
}

如果您对删除的值不感兴趣,只需调用
IDictionary.Remove(key)
。它是隐藏的,所以您必须显式地调用它

例如:

var dict = new ConcurrentDictionary<string, string>();
dict.AddOrUpdate("mykey", (val) => "test", (val1, val2) => "test");
((IDictionary)dict).Remove("mykey");
var dict=新的ConcurrentDictionary();
dict.AddOrUpdate(“mykey”、(val)=>“test”、(val1、val2)=>“test”);
删除(“我的钥匙”);
TryRemove(key,out value)
方法用于反馈操作是否有任何更改。使用最适合你需要的一款。

C#7添加糖

现在你可以写:

dictionary.TryRemove(entry.Key, out _); 

我们也允许将“丢弃”作为out参数,形式为a u 让您忽略您不关心的参数:

p.GetCoordinates(out变量x,out ux);//我只关心x


我认为第二个参数是必需的,因为您可能需要对要从
ConcurrentDictionary
中删除的项执行某些操作

例如,假设您有一个
ConcurrentDictionary
,其中
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

}
void Main()
{
var dict=新的ConcurrentDictionary();
dict.TryAdd(1,新MyDisposable());
dict.TryRemove(1,输出变量d);
d、 处置();
}
公共类:IDisposable{
#区域IDisposable支持
private bool disposedValue=false;//用于检测冗余调用
受保护的虚拟void Dispose(bool disposing)
{
如果(!disposedValue)
{
如果(处置)
{
//TODO:处置托管状态(托管对象)。
}
//TODO:释放非托管资源(非托管对象)并覆盖下面的终结器。
//TODO:将大字段设置为null。
disposedValue=true;
}
}
//TODO:仅当上面的Dispose(bool disposing)具有释放非托管资源的代码时,才重写终结器。
//~MyDisposable()
// {
////不要更改此代码。请将清理代码放入上面的Dispose(bool disposing)中。
//处置(虚假);
// }
//添加此代码是为了正确实现一次性模式。
公共空间处置()
{
//不要更改此代码。请将清理代码放入上面的Dispose(bool disposing)中。
处置(真实);
//TODO:如果上面覆盖了终结器,则取消对以下行的注释。
//总干事(本);
}
#端区
}

Eh。。为什么框架中没有包括这一点?我更好奇的是,为什么他们不在默认情况下简单地包含它。@JohnSmith,如果您想获取值并同时删除键,则使用TryRemove。如果你不关心这个值,那么就使用Remove。我刚刚试用了这个解决方案-从我这里获得了非常棒的1+效果,但是这段代码的工作方式对我来说是新的。您能否解释一下ConcurrentDictionary如何知道您只是通过将self作为arg传递来“扩展”它?有什么文章解释这个概念吗?@MarzSocks:这些被称为。读一读!我在多线程情况下使用ConcurrentDictionary,所以我想知道使用IDictionary的remove方法是否安全?GC在这里有什么关系?它可以收集一次
TryRemove
返回的值。该接口是纯粹的,因为它返回的项与删除的项完全相同:任何信息都不会丢失,并且根据定义该操作是可撤消的。还要考虑值类型。@CodesInChaos未使用的
输出值的GC
在ConcurrentDictionary类中没有Remove方法。@Jacob它继承自IDictionary,因此该方法必须存在。我添加了一个如何调用shadowed方法的示例。谢谢,这很有帮助。
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

}