0

Here's my code:

using System;
                    
public class Program
{
    
    private static string[] ar = new string[] {};
    
    public static void Main()
    {
        ar[0] = "hello";
        
        Console.WriteLine("Total array length: " + ar.Length);
        
    }
}

It show the error below when I run the above code:

Run-time exception (line 10): Index was outside the bounds of the array.

I thought that's how to define a dynamic array in C# but I must be missed something here.

mana
  • 1,075
  • 1
  • 24
  • 43
  • @PeterTrencansky How? – Jeppe Stig Nielsen Jan 01 '22 at 12:28
  • I still haven't found the answer. My intention is to create a dynamic array and later access to that array by using its index. I did try using the List but got an out of range error. – mana Jan 01 '22 at 12:44
  • I guess this is a limitation in C#. As a language, it should have a dynamic array and allow access to its values by reference its index. Same when adding a value to an array, you should be able to add a value to an array by using reference the next index in the array. List is not deliver this requirement. – mana Jan 01 '22 at 13:01
  • 2
    @fin Maybe you are looking for `IDictionary<>` instances. You can directly set and read any index in an `IDictionary<>` instance. – Progman Jan 01 '22 at 14:10
  • 1
    @fin A dynamic array in C# is a `List` that under the covers contains an array that it resizes as needed. – juharr Jan 01 '22 at 15:05
  • thanks @Progman - yes, Dictionary is what I'm looking for. It does what I expected. Thank you. – mana Jan 01 '22 at 15:07

3 Answers3

2

You create an empty array, that is an array with fixed length 0 and no entries.

Consider List<string> ar = new List<string>() instead.

Related thread: Dynamic array in C#


EDIT: It later turned out the asker could use a Dictionary<int, string>. For a Dictionary<,>, the set accessor of the indexer (by which I mean the syntax ar[0] = "hello") will either create a new key/value pair (0"hello"), or overwrite the value of an already existing key (0).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
1

Declaring private static string[] ar = new string[] {} actually means that you have an array of string with size of 0, i.e., empty array. C# doesn't allow to resize an array so you should initialize the array size if the length is fixed and this the reason you are getting the error Index was outside the bounds of the array. you are trying to set a value to an index which is larger then the array length.

In case the length is not fixed and you want to be dynamic, I recommend using List. Lists use arrays to store the data so you get the speed benefit of arrays with the convenience of a LinkedList by being able to add and remove items without worrying about having to manually change its size.

List<string> myList = new List<string>();
myList.Add("hello");
myList.Add("Ola");
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1
private static string[] ar = new string[] {};

The above will create an empty array of string (i.e. allowed length = 0) and hence the IndexOutOfBound exception.

When you are not certain of the size of your collection, use List. For e.g.: -

List<string> ar= new List<string>();
ar.Add("hello");
ar.Add("Ola");
Basant Khatiyan
  • 154
  • 2
  • 4