public bool Update(int id, Item item) { if (item == null) return false; // Don't allow the id to be changed. item.Id = id; bool have = _items.ContainsKey(id); if (have) { _items[id] = item; } return have; }
// Add a unique Id and insert into the Dictionary, then return the item. // Return null if the item cannot be added public Item Add(Item item) { // Note: if we knew more about Items, we could check to see if there already was a matching item in the Dictionary. // However, in this example we don't want to assume anything except the int Id. if (null == item) { return null; } item.Id = NextId(item); _items.Add(item.Id, item); return item; }
// Generating the next index is pretty simple with an int, but this makes it easier to // generate the next unique Id for any index type. private int NextId(Item item) { return _nextId++; }