Queue<T> Methods in C#
DevSniper
113 Views
Methods
- Enqueue(T) :- public void Enqueue (T item);It will add an element at the end of the Queue<T>. The value can be null or reference type. It is an O(1) Operation. ( if count is less than the capacity this operation is O(1) operation otherwise it's an O(n) operation here internal array needs to be reallocated to accommodate the new element)
- Dequeue :- public T Dequeue ();It will remove first element of the Queue<T> and return that element. it will throw InvalidOperationException if Queue<T> is empty. It's an O(1) operation.
- Peek :- public T Peek ();It will return beginning/first element of the Queue<T> without removing that. It is an O(1) Operation. and will throw InvalidOperationException if Queue<T> is Empty
- Clear :- public void Clear ();It will remove all elements from the Queue<T>. It is an O(n) operation
- Contains(T) :- public bool Contains (T item);Returns true if element is present in the Queue<T> otherwise false. It is an O(n) operation.
- CopyTo(T,int) :- public void CopyTo (T[] array, int arrayIndex);Copy Queue<T> elements into provided 1-D array from index, arrayIndex passed by params (zero-based index) . It is an O(n) operation.
- EnsureCapacity (int ) :- public int EnsureCapacity (int capacity);If the current capacity is less than provided capacity. it is increased Queue<T> capacity to at least the specified capacity.
- 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.Queue<T>.Enumerator GetEnumerator ();it will return an enumerator of the Queue<T>. it is an O(1) operation.
- 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.
- ToArray :- public T[] ToArray ();It will create a new array and copy all elements of Queue<T> into that array and return that array.
- ToString :- public virtual string? ToString ();It will return a string that represents the current object.
- TrimExcess :- public void TrimExcess ();if that number is less than 90 % of current capacity then it will sets the capacity to the actual number of elements in the Queue<T>. It is an O(n) operation.
- TryPeek(T) :- public bool TryPeek (out T result);It will return true if beginning element exist otherwise false ( if Queue<T> is empty). in out param, if beginning element present, set beginning element of the Queue<T> without removing that from Queue<T> otherwise for empty case, set default value of T. It is an o(1) operation.
- TryDequeue (T) :- public bool TryDequeue (out T result);Returns true if element present at the beginning of the Queue <T> otherwise false if Queue <T> is empty. In out param, set beginning element of the Queue <T> if element present and remove that element from the Queue <T>. Set default value of T in out param if Queue <T> is empty.