Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

C# Arrays

C# Character

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

C# ArrayList

C# Stack

C# Queue

C# HashTable

C# SortedList

Page Stats

Visitor: 131

SortedList Class

The SortedList collection stores key-value pairs similar to the Hashtable but the difference is that the items are in sorted order according to the key by default. Key must be unique and cannot be null whereas value can be null or duplicate.

Some important methods of SortedList
S.No.MethodDescription
Add(object key, object value)Add key-value pairs into SortedList.
Remove(object key)Removes element using key
RemoveAt(int index)Removes element using index
bool Contains(object key)Checks whether specified key exists in SortedList.
Add elements in SortedList

Use the Add() method to add key-value pairs into a SortedList. SortedList collection sorts the elements everytime you add the elements even if they are added randomly.

Some points to be remeber while using add method

  1. Key cannot be null but value can be null.
  2. SortedList key can be of any data type, but you cannot add keys of different data types in the same SortedList, it will throw runtime exception.
slist.Add(1, "Student1");
slist.Add(2, "Student2");
slist.Add("three", "Student3"); // Throw exception: InvalidOperationException

Example 1: Add elements in SortedList and display using for loop and foreach loop.

SortedList slist1 = new SortedList();
slist.Add(1, "Student1");
slist.Add(2, "Student2");
slist.Add(3, "Student3");
slist.Add(5, "Student5");
slist.Add(4, "Student4");

SortedList slist2 = new SortedList();
slist2.Add("one", "Student1");
slist2.Add("two", "Student2");
slist2.Add("three", "Student3");
slist2.Add("four", "Student4");

//int i = (int) slist1["one"]; // access single integer value
//string str = (string) slist2["four"]; //access string value

//SortedList sorts the key in alphabetical order
for (int i=0; i<slist.Count; i++)
{
    Console.WriteLine("key: {0}, value: {1}", slist.GetKey(i), slist.GetByIndex(i));
}

foreach(DictionaryEntry de in slist2)
{
    Console.WriteLine("key: {0}, value: {1}", de.Key , de.Value );
}
Remove element from SortedList

To remove an element from sorted list you can use method Remove() or RemoveAt()

slist2.Remove("one");
slist2.RemoveAt(0);
Check for existing key & value in SortedList

The Contains() & ContainsKey() methods determine whether the specified key exists in the SortedList collection or not.

bool Contains(object key);
bool ContainsKey(object key);

The ContainsValue() method determines whether the specified value exists in the SortedList or not.

bool ContainValue(object value);

Example 2: Check for key, value exists or not.

SortedList slist1 = new SortedList();
slist.Add(1, "Student1");
slist.Add(2, "Student2");
slist.Add(5, "Student5");
slist.Add(4, "Student4");

slist.Contains(2); // returns true
slist.Contains(3); // returns false

slist.ContainsKey(4); // returns true
slist.ContainsKey(6); // returns false

slist.ContainsValue("Student4"); // returns true
slist.ContainsValue("Student3"); // returns false

if (slist.ContainsValue("Student6"))
    Console.WriteLine("Student name already exits");
else
    slist.Add(6, "Student6");
Advertisement
Updated: 10-Sep-21