返回两个字典的方法 [英] Ways of returning two dictionaries
问题描述
这里也是支持我的说明Tupla缓慢的基准: http://www.dotnetperls.com/multiple-return-values
考虑到从使用Tuple从另一个类中的一个方法返回两个字典我试图使用KeyValuePair声明而不是Tupla,但是我收到以下错误:
不能隐式转换
'System.Tuple,System.Collections.Generic.Dictionary>
'System.Collections .Generic.KeyValuePair,System.Collections.Generic.Dictionary>'
这里是代码:
class AClass
{
字典< string,string> dictOne = new Dictionary< string,string>();
字典< string,string> dictTwo = new Dictionary< string,string>();
public KeyValuePair< Dictionary< string,string>,Dictionary< string,string>> MyMethodOne()
{
//添加项目dictOne和dictTwo
返回新的KeyValuePair<字典< string,string>,Dictionary< string,string>>(dictOne,dictTwo );
}
}
和
class BClass
{
AClass _ac = new AClass();
字典< string,string> dictThree = new Dictionary< string,string>();
字典< string,string> dictFour = new Dictionary< string,string>();
public void MyMethodTwo()
{
//这里我提到的错误
KeyValuePair<字典< string,string>,Dictionary< string,string>> calledKVP = _ac.MyMethodOne();
字典< string,string> dictThree = calledKVP.Key;
字典< string,string> dictFour = calledKVP.Value;
//此后我循环通过dictThree和dictFour,并做我需要的
}
}
恐怕这意味着它不能以Tupla方式的其他方式。
任何建议? p>
首先你可以使用 Tuple
class
public Tuple<字典< T1,T2>,字典< T1,T2>> GetDictionaries< T1,T2>()
{
var dict1 = new Dictionary< T1,T2>();
var dict2 = new Dictionary< T1,T2>();
return Tuple.Create(dict1,dict2);
}
public void Method()
{
var result = GetDictionaries(int,string);
var dict1 = result.Item1;
var dict2 = result.Item2;
}
另一个解决方案,要有多个输出参数,就是使用 out
参数
public void SetDictionaries(
out Dictionary< int, string> dict1,out dictionary< int,double> dict2)
{
dict1 = new Dictionary< int,string);
dict2 = new Dictionary< int,double);
}
public void Method()
{
Dictionary< int,string)dict1;
字典< int,double)dict2;
SetDictionaries(out dict1,out dict2);
}
With KeyValuePair
public KeyValuePair< Dictionary< int,int>,Dictionary< string,string>> GetDictionaries()
{
var dict1 = new Dictionary< int,int>();
var dict2 = new Dictionary< string,string>();
返回新的KeyValuePair<字典< int,int>,Dictionary< string,string>>(dict1,dict2);
}
public void Method()
{
var result = GetDictionaries();
var dict1 = result.Key;
var dict2 = result.Value;
}
I've tried to return two dictionaries and succeeded but I'm not pleased with it because its slow. Here is what I've done so far with Tupla: Returning two dictionaries with Tuple from one method within another class
Here is also benchmark that supports my statement that Tupla is slow: http://www.dotnetperls.com/multiple-return-values
Considering same situation from Returning two dictionaries with Tuple from one method within another class I've tried to go with KeyValuePair declaration instead of Tupla but I get this error:
Cannot implicitly convert type 'System.Tuple,System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.KeyValuePair,System.Collections.Generic.Dictionary>'
Here is code for that:
class AClass
{
Dictionary<string, string> dictOne = new Dictionary<string, string>();
Dictionary<string, string> dictTwo = new Dictionary<string, string>();
public KeyValuePair<Dictionary<string, string>, Dictionary<string, string>> MyMethodOne()
{
//Adding items dictOne and dictTwo
return new KeyValuePair<Dictionary<string, string>, Dictionary<string, string>>(dictOne, dictTwo);
}
}
and
class BClass
{
AClass _ac = new AClass();
Dictionary<string, string> dictThree = new Dictionary<string, string>();
Dictionary<string, string> dictFour = new Dictionary<string, string>();
public void MyMethodTwo()
{
//Here I get mentioned error
KeyValuePair<Dictionary<string, string>, Dictionary<string, string>> calledKVP = _ac.MyMethodOne();
Dictionary<string, string> dictThree = calledKVP.Key;
Dictionary<string, string> dictFour = calledKVP.Value;
//After this I loop through dictThree and dictFour and do what I need
}
}
I'm afraid that this means that it can't be done other way that "Tupla way".
Any suggestions?
First you can use the Tuple
class
public Tuple<Dictionary<T1, T2>, Dictionary<T1, T2>> GetDictionaries<T1, T2>()
{
var dict1 = new Dictionary<T1, T2>();
var dict2 = new Dictionary<T1, T2>();
return Tuple.Create(dict1, dict2);
}
public void Method()
{
var result = GetDictionaries(int, string);
var dict1 = result.Item1;
var dict2 = result.Item2;
}
Another solution, to have more than one output parameter, is to use out
parameters
public void SetDictionaries(
out Dictionary<int, string> dict1, out dictionary<int, double> dict2)
{
dict1 = new Dictionary<int, string);
dict2 = new Dictionary<int, double);
}
public void Method()
{
Dictionary<int, string) dict1;
Dictionary<int, double) dict2;
SetDictionaries(out dict1, out dict2);
}
With KeyValuePair
public KeyValuePair<Dictionary<int, int>, Dictionary<string, string>> GetDictionaries()
{
var dict1 = new Dictionary<int, int>();
var dict2 = new Dictionary<string, string>();
return new KeyValuePair<Dictionary<int, int>, Dictionary<string, string>>(dict1, dict2);
}
public void Method()
{
var result = GetDictionaries();
var dict1 = result.Key;
var dict2 = result.Value;
}
这篇关于返回两个字典的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!