Queue.Equals() Method in C#


The Queue.Equals() method in C# is used to check if a queue object is equal to another queue object.

Syntax

The syntax is as follows −

public virtual bool Equals (object obj);

Above, the parameter obj is for comparison.

Example

Let us now see an example −

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      Queue<string> queue = new Queue<string>();
      queue.Enqueue("Gary");
      queue.Enqueue("Jack");
      queue.Enqueue("Ryan");
      queue.Enqueue("Kevin");
      queue.Enqueue("Mark");
      queue.Enqueue("Jack");
      queue.Enqueue("Ryan");
      queue.Enqueue("Kevin");
      Console.WriteLine(queue.Equals(queue));
      Console.Write("Count of elements = ");
      Console.WriteLine(queue.Count);
      queue.Clear();
      Console.Write("Count of elements (updated) = ");
      Console.WriteLine(queue.Count);
   }
}

Output

This will produce the following output −

True
Count of elements = 8
Count of elements (updated) = 0

Example

Let us now see another example −

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      Queue queue = new Queue();
      queue.Enqueue(100);
      queue.Enqueue(200);
      queue.Enqueue(300);
      queue.Enqueue(400);
      Queue queue2 = new Queue();
      queue2.Enqueue(100);
      queue2.Enqueue(150);
      queue2.Enqueue(300);
      queue2.Enqueue(400);
      Console.WriteLine(queue.Equals(queue2));
   }
}

Output

This will produce the following output −

False

Updated on: 03-Dec-2019

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements