Value from SortedList

In this chapter you will learn:

  1. Get value from SortedList by key indexer
  2. Get the value list from SortedList using the GetValueList() method

Get value by key indexer

using System;//from  j a  v a 2  s.  co  m
using System.Collections;

class MainClass
{
  public static void Main()
  {
    SortedList mySortedList = new SortedList();

    mySortedList.Add("NY", "New York");
    mySortedList.Add("FL", "Florida");
    mySortedList.Add("AL", "Alabama");
    mySortedList.Add("WY", "Wyoming");
    mySortedList.Add("CA", "California");

    string myState = (string) mySortedList["CA"];
    Console.WriteLine("myState = " + myState);

    // 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);
    }
  }
}

The code above generates the following result.

Get the value list

using System;// ja  va 2s.c o m
using System.Collections;

class MainClass
{

  public static void Main()
  {
    SortedList mySortedList = new SortedList();

    mySortedList.Add("NY", "New York");
    mySortedList.Add("FL", "Florida");
    mySortedList.Add("AL", "Alabama");
    mySortedList.Add("WY", "Wyoming");
    mySortedList.Add("CA", "California");

    foreach (string myKey in mySortedList.Keys)
    {
      Console.WriteLine("myKey = " + myKey);
    }

    foreach(string myValue in mySortedList.Values)
    {
      Console.WriteLine("myValue = " + myValue);
    }
    Console.WriteLine("Getting the value list");
    IList myValueList = mySortedList.GetValueList();
    foreach(string myValue in myValueList)
    {
      Console.WriteLine("myValue = " + myValue);
    }
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to create an autogrow array for char