C# Programming Language: SortedList

Creating SortedList

using System.Collections.Generic;
SortedList<int, string> mySL = new SortedList<int, string>();

Adding Elements Into The SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);
}
Output:
1: One
4: Four
17: Seventeen

Retrieving the Keys of a SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
List<int> myKeys = new List<int>(mySL.Keys);
Console.WriteLine(String.Join(" ", myKeys));
Output:
1 4 17

Retrieving the Values of a SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
List<string> myValues = new List<string>(mySL.Values);
Console.WriteLine(String.Join(" ", myValues));
Output:
One Four Seventeen

Checking the Size of The SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.Count);
Output:
3

Checking if The SortedList Contains a Key

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.ContainsKey(1));
Console.WriteLine(mySL.ContainsKey(2));
Output:
True
False

Checking if The SortedList Contains a Value

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
Console.WriteLine(mySL.ContainsValue("One"));
Console.WriteLine(mySL.ContainsValue("Two"));
Output:
True
False

Removing An Element From a SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
mySL.Remove(1);
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);
}
Output:
4: Four
17: Seventeen

Clearing out a SortedList

SortedList<int, string> mySL = new SortedList<int, string>();
mySL.Add(1, "One");
mySL.Add(4, "Four");
mySL.Add(17, "Seventeen");
mySL.Clear();
foreach(var element in mySL)
{
Console.WriteLine(element.Key + ": " + element.Value);\
}
Output:

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

{UPDATE} Dierenverstoppertje voor Kids: Free Hidden Objects Hack Free Resources Generator

How to make an authoritative game server with PubNub

HTML & CSS — How to Split a Background Into 2 Colors

How I used BuildSpec to Deploy Serverless in AWS.

Application Load Balancer and Target group attachment using Terraform

WebDev Tips - Alpha mask PNG technique

Mound Weekly Bulletin 11/26

The Robotic Process Automation. How no-code/low-code prefix can attract new value-added.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse L

Jesse L

More from Medium

C# Programming Language: Queue

Scoped, Transient and Singleton

Async programming vs Parallel programming (Basic)

1.6 C# keywords