Q: Array question.

  • Thread starter Thread starter Martin Arvidsson
  • Start date Start date
M

Martin Arvidsson

Hi all you gurus!

Whats the biggest difference between using a string[] x; or an ArrayList x;

When to use wich?

Regards
Martin
 
When you have the explicit array (string[]), anytime you pull an item out of
the array, ex:
string s = x[0];

you (and the compiler) can safely assume that x[0] is actually a string.
ArrayList on the other hand holds "System.Object" - basically ANY type.
Thus, to retrieve an item, you would need to cast it. There are a couple
ways to do this:
string s = (string)x[0];
string s = x[0] as string;

The first will throw an InvalidCastException in the event that x[0] is NOT a
string, so use it only if you are 100% completely positive the list only
holds strings. The second will simply return a 'null' instead of throwing
an exception.

Also - a string[] is a fixed size array. Once the array is created, you
cannot add new items, or remove items. With ArrayList, it dynamically
adjusts the size, so you can add/remove items as you wish.

In .NET 2.0, with the introduction of generics, you can use List<string> (or
List<int> or List<SomeObj> -- whatever you need). This is the equivalent of
ArrayList (in terms of being able to add/remove items) with the added
advantage of not needing to cast items out of it.
 
Martin,

An array is immutable, it means that at every (size) change a new one will
be created and the old one be set ready for the GC. If this is about strings
it is even more, because strings themselfes are as well immutable.

In an arraylist will only a reference to new object be added to or an old
one removed from the list.

Cor
 
Cor Ligthert said:
An array is immutable, it means that at every (size) change a new one will
be created and the old one be set ready for the GC. If this is about strings
it is even more, because strings themselfes are as well immutable.

Arrays aren't immutable - only the *size* can't change. You can still
change the contents of the array. If arrays were immutable, you
couldn't do:

x[0] = "hello";
 
Thanx for the great explanation.

Now i got it at 100%

Regards
Martin

Adam Clauss said:
When you have the explicit array (string[]), anytime you pull an item out
of the array, ex:
string s = x[0];

you (and the compiler) can safely assume that x[0] is actually a string.
ArrayList on the other hand holds "System.Object" - basically ANY type.
Thus, to retrieve an item, you would need to cast it. There are a couple
ways to do this:
string s = (string)x[0];
string s = x[0] as string;

The first will throw an InvalidCastException in the event that x[0] is NOT
a string, so use it only if you are 100% completely positive the list only
holds strings. The second will simply return a 'null' instead of throwing
an exception.

Also - a string[] is a fixed size array. Once the array is created, you
cannot add new items, or remove items. With ArrayList, it dynamically
adjusts the size, so you can add/remove items as you wish.

In .NET 2.0, with the introduction of generics, you can use List<string>
(or List<int> or List<SomeObj> -- whatever you need). This is the
equivalent of ArrayList (in terms of being able to add/remove items) with
the added advantage of not needing to cast items out of it.

--
Adam Clauss

Martin Arvidsson said:
Hi all you gurus!

Whats the biggest difference between using a string[] x; or an ArrayList
x;

When to use wich?

Regards
Martin
 
Jon,
Arrays aren't immutable - only the *size* can't change. You can still
change the contents of the array. If arrays were immutable, you
couldn't do:

As I described, however you are right, I used the wrong word, however I am
sure I am not the only one who uses this for this behaviour as well.

Cor

Jon Skeet said:
Cor Ligthert said:
An array is immutable, it means that at every (size) change a new one
will
be created and the old one be set ready for the GC. If this is about
strings
it is even more, because strings themselfes are as well immutable.

Arrays aren't immutable - only the *size* can't change. You can still
change the contents of the array. If arrays were immutable, you
couldn't do:

x[0] = "hello";
 
Cor said:
Jon,


As I described, however you are right, I used the wrong word, however I am
sure I am not the only one who uses this for this behaviour as well.

Cor

Please correct anyone you encounter that uses it incorrectly. The
concept is tricky enough without incorrect and contradicting
descriptions of it floating around.

:)
Jon Skeet said:
Cor Ligthert said:
An array is immutable, it means that at every (size) change a new one
will
be created and the old one be set ready for the GC. If this is about
strings
it is even more, because strings themselfes are as well immutable.
Arrays aren't immutable - only the *size* can't change. You can still
change the contents of the array. If arrays were immutable, you
couldn't do:

x[0] = "hello";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top