C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

SortedList: collection that stores key/value pairs


Posted Date: 29 Feb 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: 1 out of 5Points: 10



SortedList
A SortedList creates a collection that stores the key/value pairs in sorted order based on the value of the keys.

You can obtain a read-only collection of a SortedList’s keys or values by using the properties shown here:

public virtual ICollection Keys { get;}
public virtual ICollection Values { get;}


The following program demonstrates SortedList:

//Demonstrate a SortedList.

using System;
using System.Collections;

class SortedListDemo
{
public static void Main()
{
// create a sorted SortedList.
SortedList sortedList = new SortedList();

// Add elements to the table
sortedList.Add("sky" , "Blue");
sortedList.Add("Rose" , "Red");
sortedList.Add("Water" , "Transparent");
sortedList.Add("Pumpkin" , "Orange");

// Can also add by using the indexer.
sortedList["Book"] = "Written Document";

// Get a collection of the keys.
ICollection iCollection = sortedList.Keys;

// Use the keys to obtain the values.
Console.WriteLine("Contents of the list through indexer's.");
foreach(string str in iCollection)
Console.WriteLine(str + ": " +sortedList[str]);
Console.WriteLine();

//Display the List using integer indexes.
Console.WriteLine("Contents by means of the integer indexes.");
for(int i = 0; i < sortedList.Count; i++)
Console.WriteLine(sortedList.GetByIndex(i));
Console.WriteLine();

// Show integer indexes of all the entries.
Console.WriteLine("Integer indexes of all the Entries. ");
foreach(string str in iCollection)
Console.WriteLine(str + ": " + sortedList.IndexOfKey(str));
Console.Read();
}
}

The output is shown here:

Contents of the list through indexer's.
Book: Written Document
Pumpkin: Orange
Rose: Red
sky: Blue
Water: Transparent

Contents by means of the integer indexes.
Written Document
Orange
Red
Blue
Transparent

Integer indexes of all the Entries.
Book: 0
Pumpkin: 1
Rose: 2
sky: 3
Water: 4




Responses

Author: Emmanuel Mathew    14 Jun 2004Member Level: Silver   Points : 0
Is sorted List is equal to sorted Hash table? Or isthere any other difference between these two? I have heard that Hash table also works in the key / value pair theory.

Thanks in Advance

Bye...


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Queue: A First-In, First-Out List
Previous Resource: Enumeration & sorting of objects in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use