I have a class Node as below:
public class Node
{
public Dictionary<string, string> dictionary;
public Node(Dictionary<string, string> dictionary)
{
this.dictionary = dictionary;
}
public void CreateNode()
{
this.dictionary.Add("1", "String1");
Dictionary<string, string> dictionary1 = new Dictionary<string, string>();
Console.WriteLine(this.dictionary["1"]);
Node tmp = new Node(dictionary1);
tmp.dictionary = this.dictionary;
Console.WriteLine(tmp.dictionary["1"]);
tmp.AddE(tmp, "String2","2");
Console.WriteLine(this.dictionary["2"]);
}
public void AddE(Node tmp,String text,string c)
{
tmp.dictionary.Add(c,text);
}
}
Node has a dictionary with string key and value, a constructor with parameter, a method CreateNode() which adds an item to dictionary and creates another Node. Now, after tmp.dictionary = this.dictionary; another item is added at tmp.dictionary but also it's added at this.dictionary (I don't want that to happen, I'm missing smth).
Main method:
static void Main(string[] args)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Node n = new Node(dictionary);
n.CreateNode();
}
Output is:
String1
String1
String2
For this line of code Console.WriteLine(this.dictionary["2"]); it should show this error KeyNotFoundException: The given key was not present in the dictionary. because I didn't add an item with key "2" at this.dictionary. Hope I made myself clear.
Nodewhich accepts anotherNodebut doesn't do anything with the target of the method invocation is confusing things massively.