Title: Returning two dictionaries with Tuple from one method within another class
Tags: c#;tuples
Question: 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&gt;, Dictionary<string, string&gt;&gt; type of method declaration:
class AClass
Dictionary<string, string&gt; dictOne = new Dictionary<string, string&gt;();
Dictionary<string, string&gt; dictTwo = new Dictionary<string, string&gt;();
public Tuple<Dictionary<string, string&gt;, Dictionary<string, string&gt;&gt; MyMethodOne()
//Adding items dictOne and dictTwo
return new Tuple<Dictionary<string, string&gt;, Dictionary<string, string&gt;&gt;(dictOne, dictTwo);
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&gt; dictThree = new Dictionary<string, string&gt;();
Dictionary<string, string&gt; dictFour = new Dictionary<string, string&gt;();
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);
//   }
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&gt;, Dictionary<string, string&gt;&gt;?
So, how to get dictionaries from Tuple?
Comment: Read this http://msdn.microsoft.com/en-us/library/dd268536.aspx.

Here is the accepted answer: The Tuple class exposes its members in properties called "Item(Number)": http://msdn.microsoft.com/en-us/library/dd289533.aspx
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&gt;(dictionaries.Item2);
Comment for this answer: My bad(assign->add)! What I meant is after accessing dictOne just to loop with foreach through it and add itemd to dictThree. I'll update my question!
Comment for this answer: Thank you Paolo it worked like a charm and it was pretty easy job as well.
Comment for this answer: It works but it's very slow. here is my question considering this: http://stackoverflow.com/questions/13719519/ways-of-returning-two-dictionaries
Comment for this answer: @Sylca: then just use the `new Dictionary(dictionaries.Item2)` constructor, it does exactly that :)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:12/04/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><tuples>', 'Title': 'Returning two dictionaries with Tuple from one method within another class', 'ViewCount': 706, 'id': 13705852, 'user_AboutMe': '<p>A thing that ignorant sees at the end, smart sees at the beginning.</p>\n', 'user_AccountId': 1714335.0, 'user_DisplayName': 'Sylca', 'user_Reputation': 2485.0, 'user_Views': 305.0, 'user_id': 1571756.0}}