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
Check if Hashtable has a fixed size in C#
The IsFixedSize property in C# is used to check whether a Hashtable has a fixed size or not. A fixed-size Hashtable does not allow adding or removing elements, but existing elements can still be modified. Regular Hashtables created using standard constructors are not fixed-size by default.
Syntax
Following is the syntax to check if a Hashtable has a fixed size −
bool isFixed = hashtable.IsFixedSize;
Return Value
The IsFixedSize property returns a bool value −
- True − If the Hashtable has a fixed size
- False − If the Hashtable can grow or shrink dynamically
Using IsFixedSize with Regular Hashtable
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable(10);
hash.Add("1", "A");
hash.Add("2", "B");
hash.Add("3", "C");
hash.Add("4", "D");
hash.Add("5","E");
hash.Add("6", "F");
hash.Add("7", "G");
hash.Add("8","H");
hash.Add("9", "I");
hash.Add("10", "J");
Console.WriteLine("Is the Hashtable having fixed size? = " + hash.IsFixedSize);
}
}
The output of the above code is −
Is the Hashtable having fixed size? = False
Using IsFixedSize with Hashtable Display
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable();
hash.Add("One", "Katie");
hash.Add("Two", "John");
hash.Add("Three", "Barry");
hash.Add("Four", "");
hash.Add("Five","Harry");
hash.Add("Six", "F");
hash.Add("Seven", "Tom");
hash.Add("Eight","Andy");
hash.Add("Nine", "I");
hash.Add("Ten", "Tim");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Is the Hashtable having fixed size? = " + hash.IsFixedSize);
}
}
The output of the above code is −
Hashtable Key and Value pairs... One and Katie Ten and Tim Five and Harry Three and Barry Seven and Tom Two and John Four and Eight and Andy Nine and I Six and F Is the Hashtable having fixed size? = False
Creating a Fixed-Size Hashtable
To create a fixed-size Hashtable, you can use the Hashtable.Synchronized() method with a wrapper or use CollectionBase derived classes. Here's an example demonstrating the difference −
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
// Regular Hashtable
Hashtable regularHash = new Hashtable();
regularHash.Add("A", "Apple");
regularHash.Add("B", "Banana");
Console.WriteLine("Regular Hashtable IsFixedSize: " + regularHash.IsFixedSize);
// Create a read-only wrapper (which is fixed-size)
Hashtable readOnlyHash = Hashtable.ReadOnly(regularHash);
Console.WriteLine("ReadOnly Hashtable IsFixedSize: " + readOnlyHash.IsFixedSize);
Console.WriteLine("ReadOnly Hashtable IsReadOnly: " + readOnlyHash.IsReadOnly);
}
}
The output of the above code is −
Regular Hashtable IsFixedSize: False ReadOnly Hashtable IsFixedSize: True ReadOnly Hashtable IsReadOnly: True
Conclusion
The IsFixedSize property helps determine whether a Hashtable allows adding or removing elements. Regular Hashtables return False, while wrapper collections like read-only Hashtables return True. This property is useful for validating collection behavior before performing operations.
