Re: array verses ArrayList

From: David Browne (meat_at_hotmail.com)
Date: 08/21/04


Date: Fri, 20 Aug 2004 20:05:57 -0500


"Tom Jones" <tomjones16@hotmail.com> wrote in message
news:uf%23YJBxhEHA.964@TK2MSFTNGP10.phx.gbl...
> I have a class that contains a collection of reference types. This class
> needs to have a method that returns the collection to the caller.
>
> The method's signature could be either (it could also be implemented as a
> property):
>
> MyObject[] GetObjects();
>
> or
>
> ArrayList GetObjects();
>
> The caller can modify the objects that are returned (meaning the caller
> should not get a copy of the objects).
>
> The size of the collection is never going to change.
>
> Is there any reason why I would want to return the objects in an ArrayList
> verses a plain array?
>

If the size cannot change, then no, an Array of MyObject is preferable.
With an ArrayList the user will have to down-cast the objects to use them,
which is a small ugly hastle. You could implements a non-resizable
MyObjectCollection, but that seems a little silly. Even if you have to do
some resizing or array copying, remember that smallish arrays of reference
types hava a small memory footprint and are cheap to manipulate since they
are really just arrays of pointers.

David



Relevant Pages

  • Re: Roll your own std::vector ???
    ... For the sake of your comprehension, forget about pointers ... to always provide all of the overhead of a deep copy just in case that is what ... but those of reference types are simply "pointed to" by the ... | type of array element may be a value type or a reference type ??? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fastest way to pass large array to and from COM
    ... With reference types, the types have to be marshaled to the unmanaged ... With an array of reference types, an unmanaged interface thunk is ... >> reference type would be pointless, since the marshaller has to marshal ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Roll your own std::vector ???
    ... | The commonly understood basic principle where an array name is ... For the sake of your comprehension, forget about pointers ... but those of reference types are simply "pointed to" by the ... | type of array element may be a value type or a reference type ??? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About Memory Allocation and function calls
    ... function(because the caller don't know the exact size). ... how should the caller access this array for future use? ... Allocated memory remains allocated until it is explicitly freed. ... the calling function has access to. ...
    (comp.lang.c)
  • About Memory Allocation and function calls
    ... function(because the caller don't know the exact size). ... how should the caller access this array for future use? ... I consider to use mallocto allocate memory for this array, ... return the pointer to this run time allocated memory back to the ...
    (comp.lang.c)