SortedList ContainsKey() Method in C#

In this article, we will discuss the "SortedList.ContainsKey()" method in C# with its syntax, parameters, and examples.

What is the SortedList.ContainsKey() method?

The SortedList.ContainsKey() method is a function within the C# programming language. It is part of the SortedList class, which represents a collection of key/value pairs sorted by the keys. The "System.Collection namespace" contains the SortedListclass.

The list items are internally stored in two arrays, one for the keys and another for the values managed by a SortedList object. Every element is accessible as a "DictionaryEntry" key/value pair object. Although a value can be null, a key cannot be null.

The total number of elements that a SortedList object may contain is its capacity. When new elements are added to a SortedList, reallocation is automatically used to improve capacity. Using TrimToSize or individually setting the capacity property will decrease the capacity.

The SortedList class in C# arranges elements based on keys. Sorting can occur via inherent IComparable implementation or a custom IComparer provided during instantiation. This grants flexibility in sorting, accommodating both natural key order and user-defined sorting criteria, enhancing adaptability in data management. A SortedList prohibits duplicate keys in both of these situations.

According to the sort sequence, the index sequence is derived. The indexing is adjusted when an element is added, and it is added to the SortedList in the appropriate sort order. The indexing also adjusts when an element is removed. Therefore, the index of a specific key/value pair may change because elements are added or removed from the SortedList object.

Syntax:

It has the following syntax:

Return Value:

Boolean: If there is an element in the SortedList object with the given key, it returns true; if not, it returns false.

Approach:

  1. First, create a sorted list.
  2. Next, add a key and values to the sorted list.
  3. Use the ContainsKey() method to see if the specified key is in the list.
  4. Display the output.

Example:

Output:

 
The SortedList contains the following values, and they are :
INDEX	KEY	VALUE
	[0]:	10	Ten
	[1]:	20	Twenty
	[2]:	30	Thirty
	[3]:	40	Fourty
	[4]:	50	Fifty
The key "20" is present in the SortedList.
The key "70" is NOT present in the SortedList.
The value "Fourty" is present in the SortedList.
The value "Hundred" is NOT present in the SortedList.

When Sorted Lists are useful?

There are several cases in which the SortedList class may not be suitable, even if it may be a useful tool for quickly and effectively manipulating paired data.

A SortedList has to be sorted by nature. As a result, the list must automatically sort itself whenever we add or delete an element to ensure that all its elements are in the proper order. As we increase the number of elements in our list, it gets more expensive.

SortedList is a useful tool, but it should only be used for small collections that require constant sorting. Utilizing a dictionary, hash set, or regular list is more effective than sorting once as needed while working with larger collections.

Conclusion:

In conclusion, a great and memory-efficient approach to storing sorted data is through a SortedList. The ability to index and select data using keys provides us with flexibility. The SortedList object is useful in many aspects of our everyday programming syntax after we have mastered it.