Check if a SortedList object has a fixed size in C#

To check if a SortedList object has a fixed size in C#, you can use the IsFixedSize property. This property returns true if the SortedList has a fixed size and cannot dynamically grow or shrink, or false if it can be modified.

A regular SortedList object is not fixed in size by default, which means you can add or remove elements dynamically. However, you can create a fixed-size wrapper using methods like SortedList.Synchronized() combined with other collection wrappers.

Syntax

Following is the syntax for checking if a SortedList has a fixed size −

bool isFixed = sortedList.IsFixedSize;

Example - Regular SortedList

Here's an example demonstrating how to check if a regular SortedList has a fixed size −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      SortedList list = new SortedList();
      list.Add("1", "One");
      list.Add("2", "Two");
      list.Add("3", "Three");
      list.Add("4", "Four");
      list.Add("5", "Five");
      list.Add("6", "Six");
      list.Add("7", "Seven");
      list.Add("8", "Eight");
      
      Console.WriteLine("Key and Value of SortedList....");
      foreach(DictionaryEntry k in list )
         Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
      
      Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Three"));
      Console.WriteLine("The SortedList object has a fixed size? = " + list.IsFixedSize);
   }
}

The output of the above code is −

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? True
The SortedList object has a fixed size? = False

Example - Another SortedList

Let us see another example with different data −

using System;
using System.Collections;

public class Demo {
   public static void Main(){
      SortedList list = new SortedList();
      list.Add("1", "John");
      list.Add("2", "Tim");
      list.Add("3", "Karl");
      list.Add("4", "Gary");
      list.Add("5", "Katie");
      
      Console.WriteLine("Key and Value of SortedList....");
      foreach(DictionaryEntry k in list )
         Console.WriteLine("Key: {0}, Value: {1}", k.Key , k.Value );
      
      Console.WriteLine("The SortedList object has a fixed size? = " + list.IsFixedSize);
   }
}

The output of the above code is −

Key and Value of SortedList....
Key: 1, Value: John
Key: 2, Value: Tim
Key: 3, Value: Karl
Key: 4, Value: Gary
Key: 5, Value: Katie
The SortedList object has a fixed size? = False

Understanding Fixed Size Collections

In both examples above, the IsFixedSize property returns false because regular SortedList objects are dynamically resizable. You can add or remove elements at runtime, and the collection will automatically adjust its internal capacity.

If you need a fixed-size collection, you would typically use array-based structures or create wrapper classes that restrict modification operations.

Conclusion

The IsFixedSize property of SortedList in C# returns false for regular SortedList objects, indicating they can grow or shrink dynamically. This property is useful when working with different collection types where you need to determine if the collection size can be modified at runtime.

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

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements