---
title: "C# SortedList Collection"  
description: "Sample Program that uses SortedList C#"  
author: "Uttam Misra"  
published: 2012-05-18  
updated: 2020-08-11  
canonical: https://www.mindstick.com/blog/307/c-sharp-sortedlist-collection  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# C# SortedList Collection

This below example uses the parameterless constructor, the [Add](https://www.mindstick.com/forum/448/how-to-add-advertisement-functionalities-in-asp-dot-net-page) [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) [method](https://www.mindstick.com/forum/166/webservice-method), the ContainsKey and TryGetValue [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best), the [indexer](https://www.mindstick.com/blog/103/indexer-in-c-sharp), the enumerator, the IndexOfKey and IndexOfValue methods, and finally the [Count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct) property.

##### Sample Program that uses SortedList [C#]

```
using
System;using
System.Collections.Generic; class
Program{    static void Main()    {            //            // Created SortedList with keys and values.            //            SortedList<string,
int> sorted = new
SortedList<string,
int>();           
sorted.Add("Game1", 1);           
sorted.Add("Game2", 2);           
sorted.Add("Game3", 3);           
sorted.Add("Game4", 4);           
sorted.Add("Game5", 5);            //            // Test SortedList with ContainsKey method.            //            bool contains1 = sorted.ContainsKey("Game1");            Console.WriteLine("contains
Game1 = " + contains1);             //            // Use TryGetValue method.            //            int value;            if (sorted.TryGetValue("Game1",
out value))            {               
Console.WriteLine("Game1 key is = " + value);            }             //            // Use item
indexer.|            //            Console.WriteLine("Game2
key is = " + sorted["Game2"]);             //            // Loop over SortedList data.            //            foreach (var pair in sorted)            {               
Console.WriteLine(pair);            }             //            // Get index of key and then index of value.            //            int index1 = sorted.IndexOfKey("Game3");            Console.WriteLine("index
of Game3 (key) = " + index1);|            int index2
= sorted.IndexOfValue(3);            Console.WriteLine("index
of 3 (value) = " + index2);             //            // Display Count property.            //            Console.WriteLine("count
is = " + sorted.Count);        }}
```

##### Output

contains [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) = [True](https://yourviews.mindstick.com/view/81326/boycott-chinese-products-dream-will-come-true)\
Game1 key is = 1\
Game2 key is = 2\
[Game1, 1]\
[Game2, 2]\
[Game3, 3]\
[Game4, 4]\
[Game5, 5]\
[index](https://www.mindstick.com/blog/198/index-in-sql-server) of Game3 (key) = 2\
index of 3 ([value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages)) = 2\
count is = 5 \

---

Original Source: https://www.mindstick.com/blog/307/c-sharp-sortedlist-collection

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
