SortedList

Posted on Updated on

 ujiSortedList

The SortedList class is a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index. It is a combination of an array and a hash table. It contains items that can be accessed using a key or an index. If items are accessed using an index, it is an ArrayList, and if using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Methods and Properties of the SortedList Class

Property               Description

Capacity                  Gets or sets the capacity of the SortedList.

Count                       Gets the number of elements contained 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 and sets the value associated with a specific key in the SortedList.

Keys                         Gets the keys in the SortedList.

Values                      Gets the values in the SortedList.

Sr.No.                      Methods

1                                 public virtual void Add(object key, object value);

2                                 public virtual void Clear();

3                                 public virtual bool ContainsKey(object key);

4                                 public virtual bool ContainsValue(object value);

5                                 public virtual object GetByIndex(int index);

6                                 public virtual object GetKey(int index);

7                                 public virtual IList GetKeyList();

8                                 public virtual IList GetValueList();

9                                 public virtual int IndexOfKey(object key);

10                               public virtual int IndexOfValue(object value);

11                               public virtual void Remove(object key);

12                               public virtual void RemoveAt(int index);

13                               public virtual void TrimToSize();

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 the keys.

ICollection key = sl.Keys;

foreach (string k in key)

{

Console.WriteLine(k + “: ” + sl[k]);

}

}

}

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s