Home > Code > Collections > SortedList collection in C#.NET

SortedList collection in C#.NET

by Thamilselvanj   on Dec 20, 2011   Category: Collections   |  Views: 93    |  Points: 25   |  Silver 



The System.Collections namespace provides the SortedList class used to store the items using key value pairs method and supports the item sorting. It requires the less memory to store and taking more time to search the items.

using System;
using System.Collections;


private void SortedList()
{
SortedList SrtdLst = new SortedList();
SrtdLst.Add(1, "B");
SrtdLst.Add(3, "W");
SrtdLst.Add(2, "T");
SrtdLst.Add(6, "P");
SrtdLst.Add(7, "Q");
SrtdLst.Add(5, "K");
SrtdLst.Add(4, "S");
foreach (string testString in SrtdLst.Values)
{
Response.Write(testString+"<br>");
}
bool MatchVal = SrtdLst.ContainsValue("S");
Response.Write("Sorted List contains value of Z : "+ MatchVal + "<br>");
bool MatchKey = SrtdLst.ContainsKey(7);
Response.Write("Sorted List contains Key of 7 : " + MatchKey + "<br>");
}


Output
--------------

B
T
W
S
K
P
Q
Sorted List contains value of Z : True
Sorted List contains Key of 7 : True

Like this code? Bookmark and Share:

  User Responses | Post Code
 No Response found!
  Recent Posts
* Setting focus to textbox control using JavaScript (308) by Narain_Siddharth on Nov 13 2010 11:25AM
* Sending Mails with Attachements using Asp.Net (362) by Narain_Siddharth on Nov 13 2010 11:48AM
* Disable the Submit button when insert or update a record using JavaScript (278) by Narain_Siddharth on Nov 13 2010 12:31PM
* String comparisons using C#.NET string.compareTo (289) by Thamilselvanj on Nov 14 2010 11:16AM
* String.Split using C# (287) by Thamilselvanj on Nov 14 2010 11:23AM
  Submit feedback about this code snippet
Please sign in to post feedback