Array alternatives

Posted: (EET/GMT+2)

 

As I'm learning .NET programming, I've often wanted to add elements to an array once it has been constructed, and get the index of a given array item.

So far, I've always constructed a new array or scanned the array using a for loop, but today I noticed the System.Collections.ArrayList is just what I have been looking for.

With it, you can add elements freely, and also remove them.

For searching, I noticed that you can use the System.Array class, as everything in .NET is actually an object.

So, to get the index of, say, a string in an array, you could use the following construct in C#:

string[] myArr = new ...;
int index = Array.IndexOf(
  myArr, "lookup value");
if (index >= 0)
{
  // found
  ...
}

Quite easy, once you know it, right?