Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.Equals() Method in C#
The Queue.Equals() method in C# is used to determine whether the specified object is equal to the current queue instance. This method performs reference equality comparison, not content comparison, meaning it returns true only if both variables refer to the same queue object in memory.
Syntax
Following is the syntax for the Queue.Equals() method −
public virtual bool Equals(object obj);
Parameters
obj − The object to compare with the current queue instance.
Return Value
Returns true if the specified object is the same instance as the current queue; otherwise, false.
Using Equals() for Reference Comparison
The Equals() method checks if two queue references point to the same object, not whether their contents are identical −
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");
// Same reference comparison
Console.WriteLine("queue.Equals(queue): " + queue.Equals(queue));
// Create another queue with same content
Queue<string> queue2 = new Queue<string>();
queue2.Enqueue("Gary");
queue2.Enqueue("Jack");
queue2.Enqueue("Ryan");
queue2.Enqueue("Kevin");
// Different reference comparison
Console.WriteLine("queue.Equals(queue2): " + queue.Equals(queue2));
// Assign same reference
Queue<string> queue3 = queue;
Console.WriteLine("queue.Equals(queue3): " + queue.Equals(queue3));
}
}
The output of the above code is −
queue.Equals(queue): True queue.Equals(queue2): False queue.Equals(queue3): True
Using Equals() with Non-Generic Queue
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(200);
queue2.Enqueue(300);
queue2.Enqueue(400);
Console.WriteLine("Different instances with same content:");
Console.WriteLine("queue.Equals(queue2): " + queue.Equals(queue2));
Console.WriteLine("\nSame instance comparison:");
Console.WriteLine("queue.Equals(queue): " + queue.Equals(queue));
Console.WriteLine("\nComparing with null:");
Console.WriteLine("queue.Equals(null): " + queue.Equals(null));
}
}
The output of the above code is −
Different instances with same content: queue.Equals(queue2): False Same instance comparison: queue.Equals(queue): True Comparing with null: queue.Equals(null): False
Key Points
Queue.Equals()performs reference equality, not content equality.Returns
trueonly when comparing a queue with itself or another variable pointing to the same queue object.Two different queue instances with identical content will return
false.Comparing with
nullalways returnsfalse.
Conclusion
The Queue.Equals() method in C# checks for reference equality rather than content equality. It returns true only when both queue variables refer to the exact same object instance in memory, making it useful for identity checks rather than content comparison.
