Variable-length arrays in C#

August 19th, 2007    7 Comments

Because I'm most familiar with Perl programming, whenever it's been a while since I've done any C# coding I often spend time re-remembering how to do variable-length arrays. In C#, if you want to use an array you've got to say up front how many elements it's going to contain. If, later in the code, you try to assign it more items than you said it would contain, you get an error.

But often you don't know how many items an array is going to need. It may vary every time you run the program. In this situation, you need to use C#'s ArrayList type. To use this you need to add:

using System.Collections;

up top of your code. You can then create a new ArrayList instance like this:

private ArrayList ResultsArrayList = new ArrayList();

Notice, you didn't have to give an elements limit to the list.

You can then add stuff to the ArrayList:

ResultsArrayList.Add(ResultRecord);

In this example, from code I've just been writing, ResultRecord is actually an array of field values. So here I'm creating a list of results, each of which contains a set of result values. I use this to capture search results where I know there's a fixed set of result fields for each search result (so I can use a simple array for each hit), but I don't know how many hits are going to come back from the search (so I need to use an ArrayList for the results set).

In the following code, to output my ArrayList of Arrays to the console, I do a for loop through each item in the ArrayList and within that (for each individual search result) I do a foreach loop to print out each item (or field) of data for that hit:

for (int i=0; i < ResultsArrayList.Count; i++)
{
    Console.WriteLine("HIT {0}:", i);
    // Note: ResultsArrayList[i] needs to be cast as a string[] before it can be assigned:
    string[] newarray = (string[])ResultsArrayList[i];
    foreach (string str in newarray)
    {
        Console.WriteLine("ITEM: {0}", str);
    }
}

The trouble with C# is that it's really difficult to Google for it. Google for "C#" and you get any page containing "C"! I've I've really got to try and remember about ArrayLists for when I do more C# stuff in 6 months or a year, to save myself trawling through pages and pages of stuff on arrays - none of which start with a simple redirection like: "If you don't know how many elements your array is going to need, use an ArrayList rather than an array."

Comments

  1. User Gravatar Gordon Robinson said:

    April 13th, 2009 at 6:07 pm (#)

    You probably want to take a look at generic collections to enable strong typing of the items within it.

  2. User Gravatar Alistair said:

    April 19th, 2009 at 9:31 pm (#)

    You what?!!

    ;-)

  3. User Gravatar NoCode said:

    August 19th, 2009 at 5:49 pm (#)

    Thank you verry much, was verry helpfull. Also comes in handy, because you can combine more kinds of classes (ints, strings, custome classes all together). 

  4. User Gravatar puneet singla said:

    January 12th, 2010 at 1:04 pm (#)

    thanks for this infomation

  5. User Gravatar kommradHomer said:

    August 4th, 2010 at 12:20 pm (#)

    use csharp instead of "c#"

  6. User Gravatar kavita said:

    September 7th, 2011 at 11:35 am (#)

    i wanna brief description of vairable length in c# and also wanna know thier use

  7. User Gravatar kamini said:

    September 7th, 2011 at 11:37 am (#)

    when would i get my answer

Leave a comment