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
What is the IsFixedSize property of Hashtable class in C#?
The IsFixedSize property of the Hashtable class in C# returns a bool value indicating whether the Hashtable has a fixed size. When this property returns false, you can add or remove elements. When it returns true, the size is fixed and you cannot add or remove elements.
For a regular Hashtable created using the default constructor, IsFixedSize always returns false, meaning it can grow dynamically as you add elements.
Syntax
Following is the syntax to check if a Hashtable has a fixed size −
bool isFixed = hashtable.IsFixedSize;
Return Value
The IsFixedSize property returns −
-
true− if theHashtablehas a fixed size -
false− if theHashtablecan grow or shrink dynamically
Using IsFixedSize with Regular Hashtable
Example
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("D01", "Finance");
ht.Add("D02", "HR");
ht.Add("D03", "Operations");
Console.WriteLine("IsFixedSize = " + ht.IsFixedSize);
Console.WriteLine("Count = " + ht.Count);
// Add more elements to verify it can grow
ht.Add("D04", "Marketing");
Console.WriteLine("After adding D04:");
Console.WriteLine("Count = " + ht.Count);
Console.WriteLine("IsFixedSize = " + ht.IsFixedSize);
}
}
The output of the above code is −
IsFixedSize = False Count = 3 After adding D04: Count = 4 IsFixedSize = False
Creating a Fixed-Size Hashtable
You can create a fixed-size Hashtable using the Hashtable.Synchronized() method with a read-only wrapper or by using collection wrappers −
Example
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
Hashtable originalHt = new Hashtable();
originalHt.Add("A", "Apple");
originalHt.Add("B", "Banana");
// Create a read-only wrapper
Hashtable readOnlyHt = Hashtable.ReadOnly(originalHt);
Console.WriteLine("Original Hashtable:");
Console.WriteLine("IsFixedSize = " + originalHt.IsFixedSize);
Console.WriteLine("IsReadOnly = " + originalHt.IsReadOnly);
Console.WriteLine("\nRead-only Hashtable:");
Console.WriteLine("IsFixedSize = " + readOnlyHt.IsFixedSize);
Console.WriteLine("IsReadOnly = " + readOnlyHt.IsReadOnly);
}
}
The output of the above code is −
Original Hashtable: IsFixedSize = False IsReadOnly = False Read-only Hashtable: IsFixedSize = True IsReadOnly = True
Comparison
| Hashtable Type | IsFixedSize | Can Add/Remove |
|---|---|---|
| Regular Hashtable | False | Yes |
| Read-only Hashtable | True | No |
| Synchronized Hashtable | False | Yes |
Conclusion
The IsFixedSize property helps determine whether you can modify a Hashtable by adding or removing elements. Regular Hashtable instances return false, while read-only wrappers return true. This property is useful for validating collection capabilities before attempting modifications.
