c# - 将并发字典传递给另一个方法

标签 c# concurrentdictionary

我在将 ConcurrentDictionary 传递到另一个带有 out 参数的方法时遇到一些问题。

在主方法中,

Method1(1,2,dictionary);

public override int Method1(int x,int y, out ConcurrentDictionary<string,int> dictionary)
{
  if(dictionary.IsEmpty)
  {
   do something
  }
}

我收到的错误消息是“使用未分配的输出参数字典”。我需要在整个代码中保留字典的内容。感谢您的帮助。

最佳答案

你认为“out”是什么意思? “out”有点像“ref”。 “ref”和“out”与.NET一起使用reference types 。 “ref”表示该方法可以更改变量引用的对象。即更改变量指向的内存块。 “out”表示期望该方法将定义变量引用的对象。

即如果没有参数,您必须在方法中实例化参数的实例

例如

public override int Method1(int x,int y, out ConcurrentDictionary<string,int> dictionary)
{
    dictionary = new ConcurrentDictionary<string,int>();
    // It doesn't make sense to check if it is empty here as it will always be empty
    // if(dictionary.IsEmpty)
    //  {

关于c# - 将并发字典传递给另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062998/

相关文章:

c# - 必应搜索 API 示例代码不起作用

c# - 对 ConcurrentDictionary 的线程安全更改

c# - ConcurrentDictionary GetOrAdd 异步

c# - 为什么 ConcurrentDictionary 有 AddOrUpdate 和 GetOrAdd,而 Dictionary 没有?

linux - 将当前目录更改为 sh 的管道回显不起作用

c# - 如何合并具有重叠行的 deedle 中的数据帧?

c# - 在多语言计算机中检测当前键盘语言/布局名称

c# - 在 C# 中,如何使用反射访问花括号构造函数?

c# - asp.net如何判断Session是否过期

c# - 有界阻塞集合会在广告期间丢失数据吗