0

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.

3
  • 1
    Your code is really obfuscated. The fact that you've got two different dictionaries involved, and that you've got an instance method in Node which accepts another Node but doesn't do anything with the target of the method invocation is confusing things massively. Commented Jul 3, 2016 at 16:27
  • 1
    Asteam please do not add answer into question - if you think existing answers didn't solve problem post your own answer as separate answer Commented Jul 3, 2016 at 18:39
  • And just code wouldn't really be an answer to why you're seeing the current behaviour anyway... Commented Jul 3, 2016 at 18:42

1 Answer 1

7

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, understood you perfectly. I managed to solve it like this (added answer to my question).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.