Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C# SortedList Example
3.
Methods and Properties of the SortedList Class
4.
FAQs
4.1.
What is SortedList?
4.2.
What are the types of classes in C#?
4.3.
Can a sorted list have duplicate keys?
4.4.
What is ArrayList C#?
4.5.
What is a collection class C#?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# SortedList Class

Author Shivam Verma
0 upvote

Introduction

In C#, the SortedList class represents a collection of key/value pairs sorted by the keys and accessible by key and index. SortedList in C# is a generic collection defined as SortedList<TKey, TValue>, where Tkey is the collection's key type, and Tvalue is the collection's value type.

A sorted list is a hash table and an array combined. It includes a list of items that can be accessed using a key or index. It's an ArrayList if you access items by index, and it's a Hashtable if you access items by key. The key value is always used to sort the collection of items.

Recommended Topic, Palindrome in C#, singleton design pattern in c# and Ienumerable vs Iqueryable

C# SortedList Example

Here is a C# program to illustrate how to create a sortedList.

using System;
using System.Collections;
class Codingninjas
{
    static void Main(string[] args)
    {
        SortedList sorted_list=new SortedList();
        sorted_list.Add("1.00", "Rishi");
        sorted_list.Add("2.00", "Alok");
        sorted_list.Add("3.00", "Priyanshu");
        sorted_list.Add("4.00", "Ayush");
        sorted_list.Add("5.00", "Mohini");
        if (sorted_list.ContainsValue("Javed"))
        {
            Console.WriteLine("This student name is already exist in the list");
        }
        else 
        {
            sorted_list.Add("6.00", "Javed");
        }
        // getting a collection of the keys. 
        ICollection key=sorted_list.Keys;
        foreach (string str in key)
        {
            Console.WriteLine(str+": "+sorted_list[str]);
        }
    }
}

Output 

1.00: Rishi
2.00: Alok
3.00: Priyanshu
4.00: Ayush
5.00: Mohini
6.00: Javed

Methods and Properties of the SortedList Class

The table below lists some of the SortedList class's most commonly used properties.

Here is a C# program to illustrate the properties of SortedList Class.

using System;
using System.Collections;
class Codingninjas
{
    public static void Main()
    {
        SortedList sorted_list=new SortedList();
        sorted_list.Add("1.00", "Rishi");
        sorted_list.Add("2.00", "Alok");
        sorted_list.Add("3.00", "Priyanshu");
        sorted_list.Add("4.00", "Ayush");
        sorted_list.Add("5.00", "Mohini");
        Console.WriteLine(sorted_list.IsFixedSize);
        Console.WriteLine(sorted_list.Count);
        Console.WriteLine(sorted_list.IsReadOnly);
    }
}

Output

False
5
False

The table below lists some of the SortedList class's most commonly used methods.

Here is a C# program to illustrate some methods of SortedList Class.

using System;
using System.Collections;
class Codingninjas
{
    public static void Main()
    {
        SortedList sorted_list=new SortedList();
        sorted_list.Add("1.00", "Rishi");
        sorted_list.Add("2.00", "Alok");
        sorted_list.Add("3.00", "Priyanshu");
        sorted_list.Add("4.00", "Ayush");
        sorted_list.Add("5.00", "Mohini");
        Console.WriteLine(sorted_list.ContainsValue(null));
        Console.WriteLine(sorted_list.Count);
        sorted_list.Clear();
        Console.WriteLine(sorted_list.Count);
    }
}

Output

False
5
0

 

Must Read Difference between ArrayList and LinkedList

FAQs

What is SortedList?

A sorted list is a hash table and an array combined. It includes a list of items that can be accessed using a key or index. It's an ArrayList if you access items by index, and it's a Hashtable if you access items by key. The key value is always used to sort the collection of items.

What are the types of classes in C#?

Types of classes in C# are

  • Abstract class
  • Partial class
  • Sealed class
  • Static class

Can a sorted list have duplicate keys?

A SortedList does not allow duplicate keys.

What is ArrayList C#?

The ArrayList is a non-generic group of objects in C# that increases in size dynamically. It's similar to Array, except that its size increases with time. An ArrayList can be used to add unseen data if the types and size of the data are unknown.

What is a collection class C#?

Data storage and retrieval are handled via collection classes. These classes can support stacks, queues, lists, and hash tables.  

Conclusion

In this article, we have extensively discussed the C# SortedList Class. We also discussed the methods and properties of the SortedList class.

We hope that this article has helped you enhance your knowledge regarding the C# SortedList Class and if you would like to learn more, check out our article on the Differences Between C++ and C#.

Recommended Problem - Merge K Sorted Arrays

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass