Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Array sizing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neil B - 28 Mar 2008 21:06 GMT
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??

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
Jeroen Mostert - 28 Mar 2008 21:38 GMT
> 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.

Ignacio Machin ( .NET/ C# MVP ) - 28 Mar 2008 21:41 GMT
> 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.
Peter Duniho - 28 Mar 2008 21:47 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.