.Net framework ArrayList IsFixedSize property allow us to get a value indicating whether the ArrayList has a fixed size. the arraylist IsFixedSize property exists in System.Collections namespace. this property has no optional or required parameter.
arraylist IsFixedSize property return value type is System.Boolean. this property boolean value return ‘true’, if the arraylist has a fixed size; otherwise it returns ‘false’. this property default value is ‘false’. the arraylist IsFixedSize property implements as IList.IsFixedSize.
a fixed size arraylist does not allow the addition or removal of elements after the arraylist (collection) is created. but fixed size arraylist allow the modification (update) of existing elements.
the following console c# example code demonstrate us how can we determine whether an arraylist has a fixed size programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Blue", "SkyBlue", "LightBlue" }; Console.WriteLine("ArrayList Elements... "); foreach (string color in colors) { Console.WriteLine(color); } ArrayList fixedSizeColors = ArrayList.FixedSize(colors); Console.WriteLine("\n'colors' ArrayList FixedSize? " + colors.IsFixedSize.ToString()); Console.WriteLine("Is 'fixedSizeColors' ArrayList FixedSize? " + fixedSizeColors.IsFixedSize.ToString()); colors.Add("Green"); //Uncomment this line to get error message, because fixed size ArrayList does not support add/remove //fixedSizeColors.Add("Green"); Console.WriteLine("\nAfter Adding 'Green', Now 'colors' ArrayList Elements... "); foreach (string color in colors) { Console.WriteLine(color); } fixedSizeColors[1] = "Crimson"; Console.WriteLine("\nAfter modify 'index 1', Now 'fixedSizeColors' ArrayList Elements..."); foreach (string color in fixedSizeColors) { Console.WriteLine(color); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ArrayList Elements... Blue SkyBlue LightBlue 'colors' ArrayList FixedSize? False Is 'fixedSizeColors' ArrayList FixedSize? True After Adding 'Green', Now 'colors' ArrayList Elements... Blue SkyBlue LightBlue Green After modify 'index 1', Now 'fixedSizeColors' ArrayList Elements... Blue Crimson LightBlue Green |