![]() | ![]() |
It is straight forward to create a SortedList in C#. You have to use the new keyword followed by the class name SortedList() as shown below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
SortedList slist = new SortedList();
Console.ReadLine();
}
}
}
In the script above, we create a sorted list named “slist”. You can also add, items to a sorted list while creating the list itself. Have a look at the script below:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
SortedList slist = new SortedList() {
{2, "Red"},
{4, "Blue"},
{5, "Green"},
{1, "Orange"},
{3, "Pink"}
};
Console.ReadLine();
}
}
}