O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1.13. Dynamic Arrays: The ArrayList Collection Class

As the lines of text are read from the file, I prefer to store them rather than process them immediately. A string array would be the container of choice to do this, but the C# array is a fixed-size container. The required size varies with each text file that is opened, so the C# array is too inflexible.

The System.Collections namespace provides an ArrayList container class that grows dynamically as we either insert or delete elements. For example, here is our earlier read loop revised to add elements to the container:

 using System.Collections; private void readFile() { ArrayList m_text = new ArrayList(); string text_line; while (( text_line = m_reader.ReadLine() ) != null ) { if ( text_line.Length ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required