LogIn
I don't have account.

Queue<T> Methods in C#

DevSniper
113 Views

Methods

  1. 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)
  2. 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.
  3. 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
  4. Clear :- public void Clear ();
    It will remove all elements from the Queue<T>. It is an O(n) operation
  5. 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.
  6. 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.
  7. 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.
  8. Equals(Object) :- public virtual bool Equals (object? obj);
    Returns true if specified object is equals to the current object otherwise false.
  9. GetEnumerator :- public System.Collections.Generic.Queue<T>.Enumerator GetEnumerator ();
    it will return an enumerator of the Queue<T>. it is an O(1) operation.
  10. GetHashCode :- public virtual int GetHashCode ();
    Returns a hash code of current object.
  11. GetType :- public Type GetType ();
    Returns the exact runtime type of current instance.
  12. MemberwiseClone :- protected object MemberwiseClone ();
    it will creates and return a shallow copy of the current Object.
  13. ToArray :- public T[] ToArray ();
    It will create a new array and copy all elements of Queue<T> into that array and return that array.
  14. ToString :- public virtual string? ToString ();
    It will return a string that represents the current object.
  15. 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.
  16. 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.
  17. 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.