Tech Off Post

Single Post Permalink

View Thread: How To Modify An Element in the Dictionary Class?
  • User profile image

    Dictionary<Foo, Bar> foobar = new Dictionary<Foo, Bar>();

    var foo = new Foo();

    var bar = new Bar();
    var bar2 = new Bar();

    foobar[foo] = bar; // invalid - foo isn't in the collection yet.

    foobar.Add(foo, bar); // correct way to add an element to the collection.

    foobar[foo] = bar2; // valid way of changing the element whose key is foo, but only once the element has been added in the first place.

    foobar.Remove(foo); // remove the item.

    Assert(foobar.Count == 0);