Dynamic array with strong types?

  • Thread starter Thread starter Vincent Finn
  • Start date Start date
V

Vincent Finn

Hi,

Is there any way of having a strongly typed dynamic array in C#?

ArrayList requires you to cast every time you want to access an
element
object[] isn't dynamic

I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

I want an equivalent to the C++ std::vector<type>

It seems like an astonishing oversight to leave that out of the
framework

Am I missing something obvious here?

Vin
 
I have resorted writing a hacky Add() function for object[] (below)

Forgot the code

private object[] AddToArray(object[] aSrc, object aNew)
{
if (aSrc.GetType() == typeof(int[]))
{
int[] aArray = new int[aSrc.GetLength(0) + 1];
aSrc.CopyTo(aArray, 0);
aSrc = aArray;
aSrc[aSrc.GetLength(0) - 1] = (int)aNew;
}
// and so on for other types
return aSrc;
}
 
Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection´s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html


Greetings,
 
Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection´s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html


Greetings,

Still seems like an very bad oversight\decision

Oh well at least I know I amn't just missing the obvious

Thanks, Vin
 
Hi Vincent,

Is there any way of having a strongly typed dynamic array in C#?
There is nothing out of the box, I'm afraid.

ArrayList requires you to cast every time you want to access an
element
object[] isn't dynamic

I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

The frame work has bas classes (e.g CollectionBase) which are meant to be
used for creating strongly typed collections. Look at
System.Collections.CollectionBase class
I want an equivalent to the C++ std::vector<type>

Generics (c# equivalents of C++'s templates) will be available in .NET 2.0.
Until then CollectionBase is the easiest way
 
Until then CollectionBase is the easiest way

Is there an advatage in Deriving from CollectionBase rather than
ArrayList?

If I derive from ArrayList I simply implement a 'new' indexer
deriving from CollectionBase leaves me having to implement everything
the implmentations are trivial but still a lot more code

Vin
 
You want strongy typed collection, right? Inheriting form ArrayList doesn't
give you that.
 
Is there an advatage in Deriving from CollectionBase rather than
ArrayList?

Collection base uses an ArrayList on the back-end so there is zero
benefit to using it, except that it is functional in adding validation
methods that don't exist on the ArrayList class.
If I derive from ArrayList I simply implement a 'new' indexer
deriving from CollectionBase leaves me having to implement everything
the implmentations are trivial but still a lot more code

It isn't difficult to write a strongly typed collection for each type of class.
In fact, I recall working on a tool back when I was the .NET QuickStarts
developer that dynamically emitted collection types scoped to a given
type. And I know there are several code template tools that would allow
you to write your collection once, and then replicate it for each type you
want to support.
 
You want strongy typed collection, right? Inheriting form ArrayList doesn't
give you that.

only in the return type from the indexer
don't care about internal structure

Vin
 
Vincent,

Yes, but in this case you'll end up having both indexers. With
CollectionBase in order to get not strongly typed indexer you need to cast
to IList (you can't get around this until generics). In your case strongly
typed and not strongly typed indexers are visible right away. Even more if
you inherit from CollectionBase you can control what is added to the
collection and throw an exception if it is not appropriate. If you
inheriting from ArrayList averyone can put any crap in your array.
The internal structure, though, in both cases is not strongly typed. So what
is easier is questionable.
 
Back
Top