The key-based I/O Collections in C# is what we call a SortedList −
SortedList<TKey,TValue>
The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. This is how both of them gets added in a SortedList −
s.Add("Sub1", "Physics");
s.Add("Sub2", "Chemistry");
s.Add("Sub3", "Biology");
s.Add("Sub4", "Java");The following is an example displaying the keys and values in a SortedList −
Example
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
SortedList s = new SortedList();
s.Add("Sub1", "Economics");
s.Add("Sub2", "Accountancy");
s.Add("Sub3", "Business Studies");
s.Add("Sub4", "English");
Console.WriteLine("Capacity = " + s.Capacity);
// get a collection of the keys.
ICollection key = s.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + s[k]);
}
}
}
}Output
Capacity = 16 Sub1: Economics Sub2: Accountancy Sub3: Business Studies Sub4: English