illustrates the use of a SortedList
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example11_8.cs illustrates the use of a SortedList
*/
using System;
using System.Collections;
public class Example11_8
{
public static void Main()
{
// create a SortedList object
SortedList mySortedList = new SortedList();
// add elements containing US state abbreviations and state
// names to mySortedList using the Add() method
mySortedList.Add("NY", "New York");
mySortedList.Add("FL", "Florida");
mySortedList.Add("AL", "Alabama");
mySortedList.Add("WY", "Wyoming");
mySortedList.Add("CA", "California");
// get the state name value for "CA"
string myState = (string) mySortedList["CA"];
Console.WriteLine("myState = " + myState);
// get the state name value at index 3 using the GetByIndex() method
string anotherState = (string) mySortedList.GetByIndex(3);
Console.WriteLine("anotherState = " + anotherState);
// display the keys for mySortedList using the Keys property
foreach (string myKey in mySortedList.Keys)
{
Console.WriteLine("myKey = " + myKey);
}
// display the values for mySortedList using the Values property
foreach(string myValue in mySortedList.Values)
{
Console.WriteLine("myValue = " + myValue);
}
}
}
Related examples in the same category