1

I'm still working on my JSON parser and writer.

Using Visual Studio Web Essentials, I have created a class diagram, which contains some arrays where I can put information, like this one:

public Channel[] channels { get; set; }

As you see, this is an array without predefined size (a dynamical one), and now I'd like to add something to this array, but this seems not that simple: this post, called "Adding values to a C# Array" mentions how to do this:

  • There are plenty of solutions in case the array size is known (which is not my case).
  • Use an intermediate container (a List) .... Sorry, I'd like to avoid copying my data back and forth, it looks like a performance nightmare.
  • Use the Add() method. I don't have an Add() method.
  • Use the Resize() method. I don't have a Resize() method.
  • Using using System.Linq;, methods like Append() become available. Sorry, but that using clause is present, but I don't have an Append() method.
  • Do some Enumarable...ToArray(): this also looks like copying data back and forth, just looking like another performance nightmare.

I can't believe that Web Essentials decided to use dynamical arrays without having a simple method in mind to manipulate those arrays. Does anybody have an idea?

Hereby an excerpt of my source code, throwing an IndexOutOfRangeException:

root.<stuff>.channels[root.<stuff>.channels.Length] = new Channel();

I checked the IsFixedSize attribute: it is true and read-only.

Oh, I'm working with Visual Studio Enterprise 2019, my .Net target framework seems to be 4.6.1, which seems to be the highest version, installed on my PC.

Thanks in advance

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    When operating with indexes it's `Length - 1` to get last. – Sinatr Jun 11 '21 at 09:27
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Sinatr Jun 11 '21 at 09:28
  • 1
    Not having used Web Essentials... it looks like you really want the property to *be* a `List` instead of an array... is there any reason you can't just change the property type? Even if tooling generated the type for you to start with, I'd expect you to be able to change it. – Jon Skeet Jun 11 '21 at 09:29
  • Arrays can be dynamic when they are created, but once created they are fixed size. So add an element you have to create a new array that's larger than the old one. – fredrik Jun 11 '21 at 09:30
  • maybe use Stack / Queue instead? – m4ngl3r Jun 11 '21 at 09:31
  • I was planning on commenting: "good luck on creating your own class which handles all those array operations, ow wait that's called `List`". But then irony struck me – Jochem Van Hespen Jun 11 '21 at 09:35
  • Have looked at Array class https://learn.microsoft.com/en-us/dotnet/api/system.array?view=net-5.0 – Umang Jun 11 '21 at 09:49
  • @JochemVanHespen: let me answer on your irony: https://github.com/madskristensen/WebEssentials2013/issues/2031 :-) – Dominique Jun 11 '21 at 12:22

1 Answers1

1

You can't resize fixed size array. The array is always has a fixed size

The best solution here is to use List<Channel> instead of Channel[].

Otherwise you can create a new array with the size of the previous plus one and set adding value by the last array index, but my opinion that it would be dirty.

Here are msdn docs about arrays: Array Here is what the say

Unlike the classes in the System.Collections namespaces, Array has a fixed capacity. To increase the capacity, you must create a new Array object with the required capacity, copy the elements from the old Array object to the new one, and delete the old Array.