Stack.Equals() Method in C#

The Stack.Equals() method in C# is used to check whether a Stack class object is equal to another object or not. This method performs reference equality, not content equality, meaning it returns true only if both variables reference the same Stack object in memory.

Syntax

Following is the syntax for the Stack.Equals() method −

public virtual bool Equals(object obj);

Parameters

obj − The object to compare with the current Stack instance.

Return Value

Returns true if the specified object is the same instance as the current Stack; otherwise, false.

Stack.Equals() Reference Equality stack1 [10, 20, 30] Memory: 0x1A2B stack2 (clone) [10, 20, 30] Memory: 0x3C4D stack1.Equals(stack2) = False

Using Stack.Equals() with Different Objects

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack1 = new Stack();
      stack1.Push(150);
      stack1.Push(300);
      stack1.Push(500);
      stack1.Push(750);
      stack1.Push(1000);
      
      Console.WriteLine("Stack1 elements...");
      foreach(int val in stack1) {
         Console.WriteLine(val);
      }
      
      Stack stack2 = new Stack();
      stack2.Push(350);
      stack2.Push(400);
      stack2.Push(500);
      stack2.Push(850);
      stack2.Push(900);
      
      Console.WriteLine("Stack2 elements...");
      foreach(int val in stack2) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nAre both the stacks equal? = " + stack1.Equals(stack2));
   }
}

The output of the above code is −

Stack1 elements...
1000
750
500
300
150
Stack2 elements...
900
850
500
400
350

Are both the stacks equal? = False

Using Stack.Equals() with Cloned Stack

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push(150);
      stack.Push(300);
      stack.Push(500);
      stack.Push(750);
      stack.Push(1000);
      
      Console.WriteLine("Original stack elements...");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements = " + stack.Count);
      
      Stack stack2 = (Stack)stack.Clone();
      Console.WriteLine("\nCloned stack elements...");
      foreach(int val in stack2) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Count of elements in cloned stack = " + stack2.Count);
      
      Console.WriteLine("\nAre both the stacks equal? = " + stack.Equals(stack2));
   }
}

The output of the above code is −

Original stack elements...
1000
750
500
300
150
Count of elements = 5

Cloned stack elements...
1000
750
500
300
150
Count of elements in cloned stack = 5

Are both the stacks equal? = False

Using Stack.Equals() with Same Reference

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack1 = new Stack();
      stack1.Push(100);
      stack1.Push(200);
      stack1.Push(300);
      
      Stack stack2 = stack1; // Same reference
      
      Console.WriteLine("Stack1 elements...");
      foreach(int val in stack1) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nAre both references equal? = " + stack1.Equals(stack2));
      Console.WriteLine("Reference equality check: " + ReferenceEquals(stack1, stack2));
   }
}

The output of the above code is −

Stack1 elements...
300
200
100

Are both references equal? = True
Reference equality check: True

How It Works

The Stack.Equals() method inherits the default behavior from Object.Equals(), which performs reference equality comparison. Even if two Stack objects contain identical elements in the same order, Equals() returns false because they are different objects in memory.

To compare Stack contents, you would need to implement your own comparison logic by iterating through both stacks and comparing their elements.

Conclusion

The Stack.Equals() method in C# performs reference equality comparison, not content comparison. It returns true only when comparing a Stack object with itself or another variable pointing to the same Stack instance in memory.

Updated on: 2026-03-17T07:04:36+05:30

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements