Re: Array and ArrayList

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 02/04/04


Date: Wed, 4 Feb 2004 09:59:01 -0500

Hi Kevin,
In addition to what the other said about the immutability of arrays and the
dynamic size of ArrayLists I would like to clarify some points:

1. Arrays are immutable. This doesn't meen that you cannot change the items
of the array, though. Unlike the strings, which are immutable also, arrays
items can be changed, but the size of the array cannot be changed.

2. As a result of this immutability looping over arrays for example are well
optimized.
example:
int[] intArr = new int[10];
ArrayList arrList = new ArrayList();
//code to initialize the array and the list

for(int i = 0; i < arrList.Count; i++)
{
.....
}
on each cycle arrList.Count method will be called (because the size is not
guarantee to be const)
-------------
for(int i = 0; i < intArr.Length; i++)
{
.....
}
intArr.Length will be called only once at the begining of the loop

for(int i = 0; i < arrList.Count; i++)
{
.....
}

3. Arrays are strongly typed.

4. Adding value types to ArraysList will cause boxing and reading
respectively will call unboxing. Arrays ,though, keeps the value type in
unboxed state. And calling some of the operations may not need any
boxing/unboxing or even copy the value in the stack; The unboxing will be
done anyways with ArrayList
Example:
struct Test
 {
  int a;
  public void Foo()
  {
   a++;
  }
  public override string ToString()
  {
   return "Test";
  }

 }

---------------
Test[] arr = new Test[1]{new Test()};

none of the following will cause the object in the array to be boxed/unboxed
or even copied in the stack
arr[0].Foo();
Console.WriteLine(arr[0].ToString());

5. Arrays can be used with P\Invoke

Maybe there is other advantages Arrays may have infornt of ArrayLists and
the other collection. My idea is to bring your attentions on them because
you might get the wrong impression that there is no need of Arrays and
ArrayLists are the better choice.

-- 
B\rgds
100
"Kevin" <anonymous@discussions.microsoft.com> wrote in message
news:4D5815EF-83D5-42BD-8AF4-60F177717D22@microsoft.com...
> Hi All,
>
> Can someone please tell me the difference between an Array and an
ArrayList, and where I would be likely to use one over the other.
>
> Thanks alot for any help


Relevant Pages

  • Re: does C# have any collection objects that support sort functionality so that I dont have to writ
    ... the IComparable interface. ... Also, since arrays are strongly typed, you know that all elements are of the ... for ArrayLists. ... The Sort() method will utilize each object's IComparable ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need advice on data structures
    ... I'm having some difficulties in deciding which type of data structure ... You need a Movie class ... You don't want ordinary arrays. ... You can do nested ArrayLists, but you don't want that either. ...
    (comp.lang.java.help)
  • Re: Will memory get over-used?
    ... or have a poor memory.. ... Note that this is talking about ARRAYS. ... array will be that of a bunch of references, ... Arrays are not the same as objects and ArrayLISTs ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Binding DataGrid To ArrayList
    ... binds to Arraylists, arrays, datatables, DataSets, and DataViews. ... an upgraded DataGrid but written from scratch. ... Arraylists of classes as ...
    (microsoft.public.dotnet.languages.vb)