How to add items to a Dictionary with C#

Add Items 

The Add method adds an item to the Dictionary collection in form of a key and a value. 

The following code snippet creates a Dictionary and adds an item to it by using the Add method. 

Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();

AuthorList.Add("Mahesh Chand", 35); 

Alternatively, we can use the Item property. If the key does not exist in the collection, a new item is added. If the same key already exists in the collection, the item value is updated to the new value. 

The following code snippet adds an item and updates the existing item in the collection. 

AuthorList["Neel Beniwal"] = 9;

AuthorList["Mahesh Chand"] = 20;

Up Next
    Ebook Download
    View all
    View all