-2

the string array which looks something like this (string[]); you can define it and then do all the other stuff but how to add some element to the start or end without having to use the its length every time?

also if you have already defined its length as such: 

(*string[] array = new string[5]*)

can you ever exceed its predefined length?

I already tried to google my problem and I have not found anything on YouTube yet, whenever I something on VS Code I get a bunch of red underlines....

  • 8
    No, you can't. Arrays are fixed-length. I suggest you look at `List`, which is designed to change size dynamically. – Jon Skeet Mar 23 '22 at 12:44
  • 1
    `string[]` is simply the wrong datastructure if you need dynamic length. Consider using for example `List` instead. – Fildor Mar 23 '22 at 12:44
  • 1
    Appending is fastest using a `StringBuilder` object. You can however only append not prepend or add somewhere in the middle. If that is what you need I would agree with @Fildor, use a `List`. – Paul Sinnema Mar 23 '22 at 12:46

1 Answers1

1

From Microsoft's Arrays (C# Programming Guide):

The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance.

C# offers other data structures that can solve your problem, List<T> being the most popular. Here's a trivial example:

var list = new List<string>() { "one", "two" };

list.Insert(0, "zero");
list.Add("three");
tymtam
  • 31,798
  • 8
  • 86
  • 126