How to add items to a Dictionary in 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 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; 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.