从另一个类中的一个方法返回两个带有元组的字典 [英] Returning two dictionaries with Tuple from one method within another class
问题描述
我有一个班级,AClass 班.在这个课程中,我正在填写两个字典,而且,我正在返回这两个字典,所以我使用了 Tuple
类型的方法声明:
I have one class, class AClass. In this class I'm filling two dictionaries and also, I'm returning these two dictionaries so I've used Tuple<Dictionary<string, string>, Dictionary<string, string>>
type of method declaration:
class AClass
{
Dictionary<string, string> dictOne = new Dictionary<string, string>();
Dictionary<string, string> dictTwo = new Dictionary<string, string>();
public Tuple<Dictionary<string, string>, Dictionary<string, string>> MyMethodOne()
{
//Adding items dictOne and dictTwo
return new Tuple<Dictionary<string, string>, Dictionary<string, string>>(dictOne, dictTwo);
}
}
在其他类中,BClass 类,我应该获取这两个字典,访问它们并将它们的条目添加到另外两个字典中:
In other class, class BClass, I should get these two dictionaries, access them and add their items to another two dictionaries:
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 should get dictionaries through Tuple
//Add items from dictOne to dictThree
//Add items from dictTwo to dictFour
//In a way
// foreach (var v in accessedDict)
// {
// dictThree.Add(v.Key, v.Value);
// }
}
}
如果 MyMethodOne
只返回一本字典,我会知道如何将项目从一本字典获取到另一本字典,但这里我有 元组,我从未使用过它,并且我不知道如何获得这两个重新调整值.我应该这样做吗?还有另一种方法,也许将方法声明为 Dictionary<;字典<字符串,字符串>,字典<字符串,字符串>>
?
If MyMethodOne
was returning only one dictionary I would know how to get items from one dictionary to another but here I have Tuple, with which I have never worked, and I don't know how how to get these two retuning values. Should I do it like this at all? Is there another way, maybe to declare method as Dictionary< Dictionary<string, string>, Dictionary<string, string>>
?
那么,如何从 Tuple 中获取字典?
So, how to get dictionaries from Tuple?
推荐答案
Tuple
类在名为Item(Number)"的属性中公开其成员:http://msdn.microsoft.com/en-us/library/dd289533.aspx
The Tuple
class exposes its members in properties called "Item(Number)": http://msdn.microsoft.com/en-us/library/dd289533.aspx
因此,您的两项元组将具有名为 Item1 和 Item2 的属性:
So, your two-items Tuple will have properties called Item1 and Item2:
var dictionaries = _ac.MyMethodOne();
// now dictionaries.Item1 = dictOne, dictionaries,Item2 = dictTwo
dictThree = dictionaries.Item1;
我不明白当您说您想分配项目"时,如果您只是想获得对字典的引用或复制它.如果要复制,请使用
I don't understand when you say that you want to "assign items" if you just want to get a reference to the dictionary or make a copy of it. If you want to make a copy, use
dictFour = new Dictionary<string, string>(dictionaries.Item2);
这篇关于从另一个类中的一个方法返回两个带有元组的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!