SortedList<TKey,TValue> Methods in C#
DevSniper
129 Views
Methods
- Add :- public void Add (TKey key, TValue value);It will add an element in the SortedList<TKey,TValue> with specified key and value. The time complexity of this method is O(n) for unsorted data. if the new element added at the end of the list it will be an O( log n) operation. if count is equal to capacity then adding new element require array resizing in this case time complexity will be O(n).
- Clear :- public void Clear ();it will remove all element from the SortedList<TKey,TValue>. it will set Count property to 0 and release references of the collection. Capacity remains unchanged that means this method will not capacity of the sorted list. The time complexity of this method is O(n)
- ContainsKey(TKey) :- public bool ContainsKey (TKey key);Returns true if the specified key is present in the SortedList<TKey, TValue> otherwise false. Time complexity of this method is O(log n)
- ContainsValue(TValue) :- public bool ContainsValue (TValue value);Retruns true if the specified value present in the SortedList<TKey, TValue> otherwise false. This method performs a linear search to find out value. Time complexity of this method is O(n)
- Remove(TKey) :- public bool Remove (TKey key)it will remove specified key element from the SortedList<TKey, TValue>. Return true if element removed successfully otherwise false, also returns false if key not present in the SortedList<TKey,TValue>. this method performs binary search to find out key in the sorted list however elements are moved up to fill the removed spot so time complexity of this method is O(n).
- RemoveAt(int) :- public void RemoveAt (int index);it will remove the element at the specified index of the SortedList<Tkey, TValue>. Element are moved up to fill in the removed spot so time complexity of this method will be O(n).
- GetKeyAtIndex(int) :- public TKey GetKeyAtIndex (int index);It will returns the TKey at the specified index of SortedList<TKey,TValue>. it will throw ArgumentOutOfRangeException if the specified index is out of range.
- GetValueAtIndex(int) :- public TValue GetValueAtIndex (int index);It will returns the TValue at the specified index of SortedList<TKey,TValue>. it will throw ArgumentOutOfRangeException if the specified index is out of range.
- SetValueAtIndex(int, TValue) :- public void SetValueAtIndex (int index, TValue value);it will update the value at the specified index of SortedList< TKey , TValue>. It will throw ArgumentOutOfRangeException if the specified index is out of range.
- IndexOfKey(TKey) :- public int IndexOfKey (TKey key);it will search specified key within the SortedList<TKey , TValue> and returns index (Zero- based index) if the specified key present in the list otherwise -1. this method performs binary search so time complexity of this method is O( log n).
- IndexOfValue(TValue) :- public int IndexOfValue (TValue value);it will return the index of first occurrence of the specified value if value exist in the SortedList<TKey,TValue> otherwise -1. this method performs linear search to found the value so time complexity of this operation is O(n).
- Equals (Object) :- public virtual bool Equals (object? obj);Returns true if specified object is equals to the current object otherwise false.
- GetEnumerator() :- public System.Collections.Generic.IEnumerator <System.Collections.Generic.KeyValuePair< TKey, TValue>> GetEnumerator ();it will return an enumerator of KeyValuePair<TKey,TValue> that iterates through the SortedList<TKey,TValue>. time complexity of this method is O(1)
- GetHashCode() :- public virtual int GetHashCode ();Returns a hash code of current object.
- GetType() :- public Type GetType ();Returns the exact runtime type of current instance.
- MemberwiseClone() :- protected object MemberwiseClone ();it will creates and return a shallow copy of the current Object.
- ToString() :- public virtual string? ToString ();It will return a string that represents the current object
- TryGetValue(TKey, TValue) :- public bool TryGetValue (TKey key, out TValue value);Returns true if the specified key is present in the SortedList<TKey,TValue> otherwise false, for out param : if key present in the SortedList<TKey,TValue>, set value in the TValue param otherwise set default value of TValue. This method performs binary search therefore time complexity of this method is O( log n)
- TrimExcess :- public void TrimExcess ();if the number of elements in SortedList<TKey , TValue>, is less than 90 % of current capacity then it will sets the capacity to the actual number of elements in the SortedList<TKey , TValue> . It is an O(n) operation.