C# C# Console

C# SortedList Examples

C# supports generic and non-generic SortedList. It is recommended to use generic SortedList<TKey, TValue> because it performs faster and less error-prone than the non-generic SortedList.

Example 1:

Output:

Example 2: SortedList.Add() method adds items to the collection. The following code snippet adds several items to the colleciton. 

Output:

Example 3: In the following example, we add an element with the number 7, so is the capacity is 8; if we add 9th elements, the capacity will be 16 (double the previous one).

Example 4:

The Count property counts the number of elements in the SortedList or we can say that Count gets the number of key/value pairs contained in the SortedList<TKey, TValue>. For example:

Output:

Example 5:

Output:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.