SortedMap Interface in C#


Java has SortedMap Interface, whereas an equivalent of it in C# is SortedList.

SortedList collection in C# use a key as well as an index to access the items in a list.

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Let us see an example to work with SortedList and display the keys −

Example

 Live Demo

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         SortedList sl = new SortedList();
         sl.Add("ST0", "One");
         sl.Add("ST1", "Two");
         sl.Add("ST2", "Three");
         ICollection key = sl.Keys;

         foreach(string k in key) {
            Console.WriteLine(k);
         }
      }
   }
}

Output

ST0
ST1
ST2

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements