SortedList c#.net
c#
//for
SortedList mySL1 = new SortedList();
mySL1.Add("1", "Book");
mySL1.Add("2", "Pen");
mySL1.Add("3", "Other");
for (int i = 0; i < mySL1.Count; i++)
{
Response.Write(string.Format("{0} - {1},", mySL1.GetByIndex(i).ToString(), mySL1.GetKey(i).ToString())); //Book - 1,Pen - 2,Other - 3,
//Add to DropDownList
DropDownList1.Items.Add(new ListItem(mySL1.GetByIndex(i).ToString(), mySL1.GetKey(i).ToString()));
}
// foreach
SortedList<int, string> mySL2 = new SortedList<int, string>();
mySL2.Add(1, "Book");
mySL2.Add(2, "Pen");
mySL2.Add(3, "Other");
foreach (var pair in mySL2)
{
Response.Write(string.Format("{0} - {1},", pair.Value.ToString(), pair.Key.ToString())); //Book - 1,Pen - 2,Other - 3,
//Add to DropDownList
DropDownList2.Items.Add(new ListItem(pair.Value.ToString(), pair.Key.ToString()));
}
SortedList<string, string> mySL3 = new SortedList<string, string>();
mySL3.Add("1", "Book");
mySL3.Add("2", "Pen");
mySL3.Add("3", "Other");
foreach (KeyValuePair<string, string> condition in mySL3)
{
Response.Write(string.Format("{0} - {1},", condition.Value, condition.Key)); //Book - 1,Pen - 2,Other - 3,
//Add to DropDownList
DropDownList3.Items.Add(new ListItem(condition.Value, condition.Key));
}
SortedList<string, string> mySL4= new SortedList<string, string>();
mySL4.Add("1", "Book");
mySL4.Add("2", "Pen");
mySL4.Add("3", "Other");
foreach (string myKey in mySL4.Keys)
{
Response.Write(string.Format("Key={0},", myKey)); //Key=1,Key=2,Key=3,
//display TextBox
TextBox1.Text += myKey;
}
foreach (string myValue in mySL4.Values)
{
Response.Write(string.Format("Value={0},", myValue)); //Value=Book,Value=Pen,Value=Other,
//display TextBox
TextBox1.Text += myValue;
}
//Contains
bool contains1 = mySL1.ContainsKey("1");
Response.Write(contains1.ToString()); //True
//Bind CheckBox
CheckBox1.Checked = contains1;
//index
int index1 = mySL1.IndexOfKey("2");
Response.Write(index1.ToString()); //1
Label1.Text = index1.ToString();
int index2 = mySL1.IndexOfValue("Pen");
Response.Write(index2.ToString()); //1
Label2.Text = index2.ToString();
//SetByIndex
mySL1.SetByIndex(0, "Paper");
Response.Write((string)mySL1.GetKey(1).ToString()); //2
Response.Write((string)mySL1.GetByIndex(0)); //Paper
//Label3.Text = (string)mySL1.GetKey(1);
Label3.Text = (string)mySL1.GetByIndex(0);
//Remove
mySL1.Remove("2");
Response.Write(mySL1.Count.ToString()); // 2
Label4.Text = mySL1.Count.ToString();