C# sortedlist


Release date:2023-09-06 Update date:2023-10-13 Editor:admin View counts:175

Label:

C# sortedlist

The SortedList class represents a series of key/value pairs sorted by key,which can be accessed by key and index.

A sorted list is a combination of arrays and hashtable. It contains a listof items that can be accessed using keys or indexes. If you use an index toaccess items, it is a dynamic arrayList, and if you use keys to access items, it is a Hashtable. Items in the collection are always sorted by key value.

Methods and properties of the SortedList class

The following table lists some common properties of SortedList class:

Attribute

Description

Capacity

Gets or sets the capacity of the SortedList.

Count

Gets the number of elements in the SortedList.

IsFixedSize

Gets a value indicating whether the SortedList has a fixed size.

IsReadOnly

Gets a value indicating whether the SortedList is read-only.

Item

Gets or sets the value related to the key specified in the SortedList.

Keys

Gets the key in SortedList.

Values

Gets the value in SortedList.

The following table lists some common methods of SortedList:

Serial number

Method name & description

1

Public virtual void Add (object key, object value); adds an element with thespecified key and value to SortedList.

2

Public virtual void Clear (); removes all elements from the SortedList.

3

Public virtual bool ContainsKey (object key); determines whether the SortedList contains the specified key.

4

Public virtual bool ContainsValue (object value); determines whether the SortedList contains the specified value.

5

Public virtual object GetByIndex (int index); gets the value at the specified index of the SortedList.

6

Public virtual object GetKey (int index); gets the key at the specified index of the SortedList.

7

Public virtual IList GetKeyList (); gets the key in SortedList.

8

Public virtual IList GetValueList (); gets the value in SortedList.

9

Public virtual int IndexOfKey (object key); returns the index of the specified key in SortedList, starting at zero.

10

Public virtual int IndexOfValue (object value); returns the index where the specified value appears for the first time in SortedList, starting at zero.

11

Public virtual void Remove (object key); removes the element with the specified key from the SortedList.

12

Public virtual void RemoveAt (int index); removes the element at the specified index of the SortedList.

13

Public virtual void TrimToSize (); sets the capacity to the actual number ofelements in the SortedList.

Example

The following example demonstrates the concept of a sorted list (SortedList):

Example

using System;
using System.Collections;
namespace CollectionsApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         SortedList sl = new SortedList();
         sl.Add("001", "Zara Ali");
         sl.Add("002", "Abida Rehman");
         sl.Add("003", "Joe Holzner");
         sl.Add("004", "Mausam Benazir Nur");
         sl.Add("005", "M. Amlan");
         sl.Add("006", "M. Arif");
         sl.Add("007", "Ritesh Saikia");
         if (sl.ContainsValue("Nuha Ali"))
         {
            Console.WriteLine("This student name is already in the
list");
         }
         else
         {
            sl.Add("008", "Nuha Ali");
         }
         // Get a collection of keys
         ICollection key = sl.Keys;
         foreach (string k in key)
         {
            Console.WriteLine(k + ": " + sl[k]);
         }
      }
   }
}

When the above code is compiled and executed, it produces the following results:

001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali

Powered by TorCMS (https://github.com/bukun/TorCMS).