Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ArrayList.IsFixedSize Property

Gets a value indicating whether the ArrayList has a fixed size.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual bool IsFixedSize { get; }
/** @property */
public boolean get_IsFixedSize ()

public function get IsFixedSize () : boolean

Not applicable.

Property Value

true if the ArrayList has a fixed size; otherwise, false. The default is false.

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.

Retrieving the value of this property is an O(1) operation.

The following code example shows how to create a fixed-size wrapper around an ArrayList.

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 )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      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()

*/ 

import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // 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.get_IsFixedSize()
            ? "has a fixed size" : "does not have a fixed size");
        Console.WriteLine("myFixedSizeAL {0}.", myFixedSizeAL.get_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 (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
        try {
            myFixedSizeAL.Insert(3, "InsertMe");
        }
        catch (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
    } //main

    public static void PrintValues(IEnumerable myList, char mySeparator)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0}{1}", new Character(mySeparator), obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 
/*
 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(String[] args)
 Exception: System.NotSupportedException: Collection was of a fixed size.
    at System.Collections.FixedSizeArrayList.Insert(Int32 index, Object obj)
    at SamplesArrayList.main(String[] args)

 */

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()
 */ 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.