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
Stack.Contains() Method in C#
The Stack.Contains() method in C# is used to check whether a specific element exists in the Stack or not. It returns true if the element is found, otherwise false.
Syntax
The syntax for the Stack.Contains() method is as follows −
public virtual bool Contains(object obj);
Parameters
obj − The object to be searched in the stack. It can be
null.
Return Value
Returns a bool value −
true − if the element is found in the Stack
false − if the element is not found in the Stack
Using Contains() with String Elements
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element 'Speakers' is in the stack? = " + stack.Contains("Speakers"));
Console.WriteLine("Element 'Alienware' is in the stack? = " + stack.Contains("Alienware"));
}
}
The output of the above code is −
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element 'Speakers' is in the stack? = False Element 'Alienware' is in the stack? = True
Using Contains() with Numeric Elements
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);
stack.Push(1250);
stack.Push(1500);
stack.Push(2000);
stack.Push(2500);
Console.WriteLine("Stack elements...");
foreach(int val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element 750 is in the stack? = " + stack.Contains(750));
Console.WriteLine("Element 5000 is in the stack? = " + stack.Contains(5000));
}
}
The output of the above code is −
Stack elements... 2500 2000 1500 1250 1000 750 500 300 150 Count of elements = 9 Element 750 is in the stack? = True Element 5000 is in the stack? = False
How It Works
The Contains() method performs a linear search through the Stack elements from top to bottom. It uses the Object.Equals() method to compare each element with the specified object. The search stops as soon as a match is found or all elements have been checked.
The time complexity is O(n) where n is the number of elements in the Stack, as it may need to examine every element in the worst case.
Conclusion
The Stack.Contains() method provides a convenient way to check if an element exists in a Stack collection. It returns a boolean value indicating whether the specified object is present, making it useful for validation and conditional operations in your applications.
