ArrayList.IsFixedSize Property
Gets a value indicating whether the ArrayList has a fixed size.
[Visual Basic] Public Overridable ReadOnly Property IsFixedSize As Boolean _ Implements IList.IsFixedSize [C#] public virtual bool IsFixedSize {get;} [C++] public: __property virtual bool get_IsFixedSize(); [JScript] public function get IsFixedSize() : Boolean;
Property Value
true if the ArrayList has a fixed size; otherwise, false. The default is false.
Implements
Remarks
A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements.
A collection with a fixed size is simply a collection with a wrapper that prevents adding and removing elements; therefore, if changes are made to the underlying collection, including the addition or removal of elements, the fixed-size collection reflects those changes.
Example
The following example shows how to create a fixed-size wrapper around an ArrayList.
[Visual Basic] Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesArrayList Public Shared Sub Main() ' Creates and initializes a new ArrayList. Dim myAL As New ArrayList() myAL.Add("The") myAL.Add("quick") myAL.Add("brown") myAL.Add("fox") myAL.Add("jumped") myAL.Add("over") myAL.Add("the") myAL.Add("lazy") myAL.Add("dog") ' Create a fixed-size wrapper around the ArrayList. Dim myFixedSizeAL As ArrayList = ArrayList.FixedSize(myAL) ' Display whether the ArrayLists have a fixed size or not. Dim msg As String If myAL.IsFixedSize Then msg = "has a fixed size" Else msg = "does not have a fixed size" End If Console.WriteLine("myAL {0}.", msg) If myFixedSizeAL.IsFixedSize Then msg = "has a fixed size" Else msg = "does not have a fixed size" End If Console.WriteLine("myFixedSizeAL {0}.", msg) Console.WriteLine() ' Display both ArrayLists. Console.WriteLine("Initially,") Console.Write("Standard :") PrintValues(myAL, " "c) Console.Write("Fixed size:") PrintValues(myFixedSizeAL, " "c) ' Sort is allowed in the fixed-size ArrayList. myFixedSizeAL.Sort() ' Display both ArrayLists. Console.WriteLine("After Sort,") Console.Write("Standard :") PrintValues(myAL, " "c) Console.Write("Fixed size:") PrintValues(myFixedSizeAL, " "c) ' Reverse is allowed in the fixed-size ArrayList. myFixedSizeAL.Reverse() ' Display both ArrayLists. Console.WriteLine("After Reverse,") Console.Write("Standard :") PrintValues(myAL, " "c) Console.Write("Fixed size:") PrintValues(myFixedSizeAL, " "c) ' Add an element to the standard ArrayList. myAL.Add("AddMe") ' Display both ArrayLists. Console.WriteLine("After adding to the standard ArrayList,") Console.Write("Standard :") PrintValues(myAL, " "c) Console.Write("Fixed size:") PrintValues(myFixedSizeAL, " "c) Console.WriteLine() ' Adding or inserting elements to the fixed-size ArrayList throws an exception. Try myFixedSizeAL.Add("AddMe2") Catch myException As Exception Console.WriteLine("Exception: " + myException.ToString()) End Try Try myFixedSizeAL.Insert(3, "InsertMe") Catch myException As Exception Console.WriteLine("Exception: " + myException.ToString()) End Try End Sub 'Main Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char) Dim myEnumerator As System.Collections.IEnumerator = _ myList.GetEnumerator() While myEnumerator.MoveNext() Console.Write("{0}{1}", mySeparator, myEnumerator.Current) End While Console.WriteLine() End Sub End Class ' This code produces the following output. ' ' myAL does not have a fixed size. ' myFixedSizeAL has a fixed size. ' ' Initially, ' Standard : The quick brown fox jumped over the lazy dog ' Fixed size: The quick brown fox jumped over the lazy dog ' After Sort, ' Standard : brown dog fox jumped lazy over quick the The ' Fixed size: brown dog fox jumped lazy over quick the The ' After Reverse, ' Standard : The the quick over lazy jumped fox dog brown ' Fixed size: The the quick over lazy jumped fox dog brown ' After adding to the standard ArrayList, ' Standard : The the quick over lazy jumped fox dog brown AddMe ' Fixed size: The the quick over lazy jumped fox dog brown AddMe ' ' Exception: System.NotSupportedException: Collection was of a fixed size. ' at System.Collections.FixedSizeArrayList.Add(Object obj) ' at SamplesArrayList.Main() ' Exception: System.NotSupportedException: Collection was of a fixed size. ' at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj) ' at SamplesArrayList.Main() [C#] using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Create a fixed-size wrapper around the ArrayList. ArrayList myFixedSizeAL = ArrayList.FixedSize( myAL ); // Display whether the ArrayLists have a fixed size or not. Console.WriteLine( "myAL {0}.", myAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" ); Console.WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" ); Console.WriteLine(); // Display both ArrayLists. Console.WriteLine( "Initially," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Sort is allowed in the fixed-size ArrayList. myFixedSizeAL.Sort(); // Display both ArrayLists. Console.WriteLine( "After Sort," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Reverse is allowed in the fixed-size ArrayList. myFixedSizeAL.Reverse(); // Display both ArrayLists. Console.WriteLine( "After Reverse," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Add an element to the standard ArrayList. myAL.Add( "AddMe" ); // Display both ArrayLists. Console.WriteLine( "After adding to the standard ArrayList," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); Console.WriteLine(); // Adding or inserting elements to the fixed-size ArrayList throws an exception. try { myFixedSizeAL.Add( "AddMe2" ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } try { myFixedSizeAL.Insert( 3, "InsertMe" ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } } public static void PrintValues( IEnumerable myList, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. myAL does not have a fixed size. myFixedSizeAL has a fixed size. Initially, Standard : The quick brown fox jumped over the lazy dog Fixed size: The quick brown fox jumped over the lazy dog After Sort, Standard : brown dog fox jumped lazy over quick the The Fixed size: brown dog fox jumped lazy over quick the The After Reverse, Standard : The the quick over lazy jumped fox dog brown Fixed size: The the quick over lazy jumped fox dog brown After adding to the standard ArrayList, Standard : The the quick over lazy jumped fox dog brown AddMe Fixed size: The the quick over lazy jumped fox dog brown AddMe Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Add(Object obj) at SamplesArrayList.Main() Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj) at SamplesArrayList.Main() */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Collections; void PrintValues( IEnumerable* myList, String* mySeparator ); int main() { String* cSeparator = " "; // Creates and initializes a new ArrayList. ArrayList* myAL = new ArrayList(); myAL->Add( S"The" ); myAL->Add( S"quick" ); myAL->Add( S"brown" ); myAL->Add( S"fox" ); myAL->Add( S"jumped" ); myAL->Add( S"over" ); myAL->Add( S"the" ); myAL->Add( S"lazy" ); myAL->Add( S"dog" ); // Creates a fixed-size wrapper around the ArrayList. ArrayList* myFixedSizeAL = ArrayList::FixedSize( myAL ); // Displays whether the ArrayLists have a fixed size or not. String* strRes = myAL->IsFixedSize ? "has a fixed size" : "does not have a fixed size"; Console::WriteLine( "myAL {0}.",strRes ); String* strFixedRes = myFixedSizeAL->IsFixedSize ? "has a fixed size" : "does not have a fixed size"; Console::WriteLine( "myFixedSizeAL {0}.", strFixedRes ); Console::WriteLine(); // Displays both ArrayLists. Console::WriteLine( "Initially," ); Console::Write( "Standard :" ); PrintValues( myAL, cSeparator ); Console::Write( "Fixed size:" ); PrintValues( myFixedSizeAL, cSeparator ); // Sort is allowed in the fixed-size ArrayList. myFixedSizeAL->Sort(); // Displays both ArrayLists. Console::WriteLine( "After Sort," ); Console::Write( "Standard :" ); PrintValues( myAL, cSeparator ); Console::Write( "Fixed size:" ); PrintValues( myFixedSizeAL, cSeparator ); // Reverse is allowed in the fixed-size ArrayList. myFixedSizeAL->Reverse(); // Displays both ArrayLists. Console::WriteLine( "After Reverse," ); Console::Write( "Standard :" ); PrintValues( myAL, cSeparator ); Console::Write( "Fixed size:" ); PrintValues( myFixedSizeAL, cSeparator ); // Adds an element to the standard ArrayList. myAL->Add( S"AddMe" ); // Displays both ArrayLists. Console::WriteLine( "After adding to the standard ArrayList," ); Console::Write( "Standard :" ); PrintValues( myAL, cSeparator ); Console::Write( "Fixed size:" ); PrintValues( myFixedSizeAL, cSeparator ); Console::WriteLine(); // Adding or inserting elements to the fixed-size ArrayList throws an exception. try { myFixedSizeAL->Add( S"AddMe2" ); } catch ( Exception* myException ) { Console::WriteLine(String::Concat("Exception: ", myException->ToString())); } try { myFixedSizeAL->Insert( 3, S"InsertMe" ); } catch ( Exception* myException ) { Console::WriteLine(String::Concat("Exception: ", myException->ToString())); } } void PrintValues( IEnumerable* myList, String* mySeparator ) { System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator(); while ( myEnumerator->MoveNext() ) Console::Write( "{0}{1}", mySeparator, myEnumerator->Current ); Console::WriteLine(); } /* This code produces the following output. myAL does not have a fixed size. myFixedSizeAL has a fixed size. Initially, Standard : The quick brown fox jumped over the lazy dog Fixed size: The quick brown fox jumped over the lazy dog After Sort, Standard : brown dog fox jumped lazy over quick the The Fixed size: brown dog fox jumped lazy over quick the The After Reverse, Standard : The the quick over lazy jumped fox dog brown Fixed size: The the quick over lazy jumped fox dog brown After adding to the standard ArrayList, Standard : The the quick over lazy jumped fox dog brown AddMe Fixed size: The the quick over lazy jumped fox dog brown AddMe Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Add(Object obj) at main() Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj) at main() */ [JScript] import System; import System.Collections; // Creates and initializes a new ArrayList. var myAL : ArrayList = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Create a fixed-size wrapper around the ArrayList. var myFixedSizeAL : ArrayList = ArrayList.FixedSize( myAL ); // Display whether the ArrayLists have a fixed size or not. Console.WriteLine( "myAL {0}.", myAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" ); Console.WriteLine( "myFixedSizeAL {0}.", myFixedSizeAL.IsFixedSize ? "has a fixed size" : "does not have a fixed size" ); Console.WriteLine(); // Display both ArrayLists. Console.WriteLine( "Initially," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Sort is allowed in the fixed-size ArrayList. myFixedSizeAL.Sort(); // Display both ArrayLists. Console.WriteLine( "After Sort," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Reverse is allowed in the fixed-size ArrayList. myFixedSizeAL.Reverse(); // Display both ArrayLists. Console.WriteLine( "After Reverse," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); // Add an element to the standard ArrayList. myAL.Add( "AddMe" ); // Display both ArrayLists. Console.WriteLine( "After adding to the standard ArrayList," ); Console.Write( "Standard :" ); PrintValues( myAL, ' ' ); Console.Write( "Fixed size:" ); PrintValues( myFixedSizeAL, ' ' ); Console.WriteLine(); // Adding or inserting elements to the fixed-size ArrayList throws an exception. try { myFixedSizeAL.Add( "AddMe2" ); } catch ( myException : Exception ) { Console.WriteLine("Exception: " + myException.ToString()); } try { myFixedSizeAL.Insert( 3, "InsertMe" ); } catch ( myException : Exception ) { Console.WriteLine("Exception: " + myException.ToString()); } function PrintValues( myList : IEnumerable, mySeparator : char ) { var myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); Console.WriteLine(); } /* This code produces the following output. myAL does not have a fixed size. myFixedSizeAL has a fixed size. Initially, Standard : The quick brown fox jumped over the lazy dog Fixed size: The quick brown fox jumped over the lazy dog After Sort, Standard : brown dog fox jumped lazy over quick the The Fixed size: brown dog fox jumped lazy over quick the The After Reverse, Standard : The the quick over lazy jumped fox dog brown Fixed size: The the quick over lazy jumped fox dog brown After adding to the standard ArrayList, Standard : The the quick over lazy jumped fox dog brown AddMe Fixed size: The the quick over lazy jumped fox dog brown AddMe Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Add(Object obj) at JScript 0.Global Code() Exception: System.NotSupportedException: Collection was of a fixed size. at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj) at JScript 0.Global Code() */
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
ArrayList Class | ArrayList Members | System.Collections Namespace