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
ContainsKey() method in C#
The ContainsKey() method in C# is used to check whether a specific key exists in a Hashtable or other collection types like Dictionary. This method returns true if the key is found, otherwise it returns false.
Syntax
Following is the syntax for the ContainsKey() method −
bool ContainsKey(object key)
Parameters
-
key − The key to locate in the collection.
Return Value
Returns true if the key exists in the collection; otherwise, false.
Using ContainsKey() with Hashtable
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable h = new Hashtable();
h.Add(1, "Sam");
h.Add(2, "Jack");
h.Add(3, "Andy");
h.Add(4, "Katie");
h.Add(5, "Beth");
h.Add(6, "Benjamin");
Console.WriteLine("Keys and Values list:");
foreach (var key in h.Keys) {
Console.WriteLine("Key = {0}, Value = {1}", key, h[key]);
}
Console.WriteLine("Does key 3 exist? " + h.ContainsKey(3));
Console.WriteLine("Does key 10 exist? " + h.ContainsKey(10));
}
}
The output of the above code is −
Keys and Values list: Key = 6, Value = Benjamin Key = 5, Value = Beth Key = 4, Value = Katie Key = 3, Value = Andy Key = 2, Value = Jack Key = 1, Value = Sam Does key 3 exist? True Does key 10 exist? False
Using ContainsKey() with Dictionary
Example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Alice", 25);
ages.Add("Bob", 30);
ages.Add("Charlie", 28);
Console.WriteLine("Dictionary contents:");
foreach (var pair in ages) {
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
string searchKey = "Bob";
if (ages.ContainsKey(searchKey)) {
Console.WriteLine("{0} is {1} years old", searchKey, ages[searchKey]);
} else {
Console.WriteLine("{0} not found", searchKey);
}
searchKey = "David";
if (ages.ContainsKey(searchKey)) {
Console.WriteLine("{0} is {1} years old", searchKey, ages[searchKey]);
} else {
Console.WriteLine("{0} not found", searchKey);
}
}
}
The output of the above code is −
Dictionary contents: Alice: 25 Bob: 30 Charlie: 28 Bob is 30 years old David not found
Practical Use Case
Example
using System;
using System.Collections.Generic;
public class StudentGrades {
public static void Main() {
Dictionary<int, string> students = new Dictionary<int, string>();
students.Add(101, "A");
students.Add(102, "B+");
students.Add(103, "A-");
int[] studentIds = {101, 104, 102, 105};
foreach (int id in studentIds) {
if (students.ContainsKey(id)) {
Console.WriteLine("Student {0}: Grade {1}", id, students[id]);
} else {
Console.WriteLine("Student {0}: No grade found", id);
}
}
}
}
The output of the above code is −
Student 101: Grade A Student 104: No grade found Student 102: Grade B+ Student 105: No grade found
Conclusion
The ContainsKey() method is essential for safely checking if a key exists before accessing its value in collections like Hashtable and Dictionary. It prevents KeyNotFoundException errors and enables conditional logic based on key presence, making your code more robust and error-free.
