> In C++ arrays have the .SetAtGrow() method that expands the array to
> accommodate the index specified.
You're talking about the MFC CArray class. That's not the same thing as a
"C++ array". C++ has plain old C arrays, but those don't grow.
> I don't find an equivalent method in C#.
>
> Is there one??
No. Array objects are immutable in C# (their contents are not, of course).
> If not, what is the recommended way to handle this??
Use a List<T> instead. (That's not a linked list, contrary to what you might
think, it's the equivalent of an STL vector.)
List<T> has no way of automatically expanding to a specified index, but it's
easy enough to achieve. Here's an extension method in C# 3 that will achieve
the same as CArray::SetAtGrow:
public static void SetAtGrow<T>(this List<T> list, int index, T item) {
if (list.Count > index) return;
if (list.Capacity < index) list.Capacity = index;
list.AddRange(Forever(default(T)).Take(index - list.Count));
list.Add(item);
}
public static IEnumerable<T> Forever<T>(T item) {
while (true) yield return item;
}
If you're going to do this a lot, you'll want to set .Capacity to a
reasonable value before, to minimize reallocations. If the list you're going
to be creating this way is very sparse, consider using a Dictionary<int, T>
instead.
> Currently I'm using the Resize(....) method but this can be awkward at times.
I'll say. It'll be especially awkward for performance. .Resize() does not
actually resize an array, it creates a brand new one -- the documentation
explains this.

Signature
J.
Jeroen Mostert - 28 Mar 2008 21:42 GMT
> public static void SetAtGrow<T>(this List<T> list, int index, T item) {
> if (list.Count > index) return;
> if (list.Capacity < index) list.Capacity = index;
> list.AddRange(Forever(default(T)).Take(index - list.Count));
> list.Add(item);
> }
Heh. That's what you get for modifying your code halfway through. The
obvious fix:
public static void SetAtGrow<T>(this List<T> list, int index, T newElement) {
if (index < list.Count) {
list[index] = newElement;
return;
}
if (list.Capacity < index) list.Capacity = index;
list.AddRange(Forever(default(T)).Take(index - list.Count));
list.Add(newElement);
}

Signature
J.
Jeroen Mostert - 28 Mar 2008 21:44 GMT
>> public static void SetAtGrow<T>(this List<T> list, int index, T item) {
>> if (list.Count > index) return;
[quoted text clipped - 16 lines]
> list.Add(newElement);
> }
*sigh* The above code still contains one more bug, but this one is left as
an exercise to the reader, as I obviously need some refreshments.

Signature
J.
> In C++ arrays have the .SetAtGrow() method that expands the array to
> accommodate the index specified. I don't find an equivalent method in C#.
>
> Is there one??
No
> If not, what is the recommended way to handle this??
> Currently I'm using the Resize(....) method but this can be awkward at times.
> Thanks, Neil
Use a List<T> collection.
> In C++ arrays have the .SetAtGrow() method that expands the array to
> accommodate the index specified.
No they don't. Certain array templates have that method, but C++ arrays
themselves don't support that.
> I don't find an equivalent method in C#.
>
> Is there one??
Not really, no. You found Array.Resize(), which is about as close as
you're going to get with the actual Array class.
> If not, what is the recommended way to handle this??
If you are specifically needing to expand an array to accomodate a new
element at some index farther into the index space of the array than just
the next element after the current end of the array, I think
Array.Resize() is your best bet. There's the List<T> class, but it only
supports dynamic expansion one element at a time.
I suppose if you have a need to do this on a regular basis, you could
implement your own generic collection class that worked like List<T> but
which had a settable Count property. I don't think that'd actually be all
that hard to write and it would address your specific need.
Pete