我在将 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/