C# object instance not working properly

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.

Jon Skeet
people
quotationmark

because I didn't add an item with key "2" at this.dictionary.

Yes you did. You did it with this call:

tmp.AddE(tmp, "String2","2");

That adds an entry with key "2" and value "String2" to the dictionary referred to by tmp... which is the same dictionary that this refers to due to this line:

tmp.dictionary = this.dictionary;

The second dictionary that you created with the line:

Dictionary<string, string> dictionary1 = new Dictionary<string, string>();

... is eligible for garbage collection, because nothing refers to it afterwards. It's initially the dictionary in the new tmp node, but then you replace that with the reference to the same dictionary in "this", as shown above.

Side-note: try to avoid constructing such confusing code. A method like this is a recipe of problems:

public void AddE(Node tmp,String text,string c)
{
    tmp.dictionary.Add(c,text);
}   

That's an instance method, but it doesn't use any of the state of the "current" instance (that this refers to) - instead, it modifies the state of the node that is passed in. It would be better written as:

public void AddE(String text, string c)
{
    this.dictionary.Add(c,text);
} 

... in other words, modifying the state of this instead. (It's still odd passing the key after the value, and a parameter called c provides no hint as to what it's meant to be for, but that's a different matter.)

people

See more on this question at Stackoverflow