Here we see the SortedDictionary collection from System.Collections.Generic being used. We add 5 keys in any order, being careful not to add duplicates.
using System;
using System.Collections.Generic;
var sort = new SortedDictionary<string, int>();
// Add strings and int keys.
sort.Add(
"zebra", 5);
sort.Add(
"cat", 2);
sort.Add(
"dog", 9);
sort.Add(
"mouse", 4);
sort.Add(
"programmer", 100);
// See if it doesn't contain "dog."
if (sort.ContainsKey(
"dog"))
{
Console.WriteLine(true);
}
// See if it contains "zebra."
if (sort.ContainsKey(
"zebra"))
{
Console.WriteLine(true);
}
// See if it contains "ape."
Console.WriteLine(sort.ContainsKey(
"ape"));
// See if it contains "programmer", and if so get the value.
int v;
if (sort.TryGetValue(
"programmer", out v))
{
Console.WriteLine(v);
}
// Print SortedDictionary in alphabetic order.
foreach (KeyValuePair<string, int> p in sort)
{
Console.WriteLine(
"{0} = {1}", p.Key, p.Value);
}
True
True
False
100
cat = 2
dog = 9
mouse = 4
programmer = 100
zebra = 5