How to add items to a SortedDictionary with C#

Add Items

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


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


SortedDictionary<string, Int16> AuthorList = new SortedDictionary<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;



Learn more:

Working with SortedDictionary using C#

Up Next
    Ebook Download
    View all
    View all