1

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

在主要方法中,

Method1(1,2,dictionary);

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

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

4

2 回答 2

1

你认为“out”是什么意思?“out”有点像“ref”。“ref”和“out”适用于 .NET引用类型。“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)
    //  {
于 2013-11-19T04:10:09.133 回答
0

因为dictionary是一个out参数,所以你必须保证dictionary在时间Method1完成之前分配给它。如果你不想改变dictionary,你可以把它分配给它自己。

于 2013-11-19T04:12:02.673 回答