3

How can I define a dynamic array in C#?

0

7 Answers 7

15

C# doesn't provide dynamic arrays. Instead, it offers List class which works the same way.

To use lists, write at the top of your file:

using System.Collections.Generic;

And where you want to make use of a list, write (example for strings):

List<string> mylist = new List<string>();
mylist.Add("First string in list");
Sign up to request clarification or add additional context in comments.

7 Comments

This isn't really an array per se, but rather a collection. Although it can be accessed like an array, in memory it's stored as a linked list.
I don't think a List is internally stored as a linked list. It has to be as an array.
No, a List is a linked list internally. What makes you think it must be an array?
List<T> is using an array internally. You can find this either with Reflector or in the documentation.
List<T> is not a linked list. LinkedList<T> is a linked list.
|
8

Take a look at Array.Resize if you need to resize an array.

    // Create and initialize a new string array.
    String[] myArr = {"The", "quick", "brown", "fox", "jumps", 
        "over", "the", "lazy", "dog"};

    // Resize the array to a bigger size (five elements larger).
    Array.Resize(ref myArr, myArr.Length + 5);

    // Resize the array to a smaller size (four elements).
    Array.Resize(ref myArr, 4);

Alternatively you could use the List class as others have mentioned. Make sure you specify an initial size if you know it ahead of time to keep the list from having to resize itself underneath. See the remarks section of the initial size link.

    List<string> dinosaurs = new List<string>(4);

    Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

    dinosaurs.Add("Tyrannosaurus");
    dinosaurs.Add("Amargasaurus");
    dinosaurs.Add("Mamenchisaurus");
    dinosaurs.Add("Deinonychus");

If you need the array from the List, you can use the ToArray() function on the list.

    string[] dinos = dinosaurs.ToArray();

Comments

1

C# does provide dynamic arrays and dynamic array manipulation. The base of an array is dynamic and can be modified with a variable. You can find the array tutorial here (https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx). I have also included code that demonstrates an empty set array and a dynamic array that can be resized at run time.

class Program
{
    static void Main(string[] args)
    {
        int x = Convert.ToInt32(Console.ReadLine());
        int y = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine(x);
        {


            int[] dynamicArray1 = { };//empty array
            int[] numbers;//another way to declare a variable array as all arrays start as variable size
            numbers = new int[x];//setting this array to an unknown variable (will be user input)

            for (int tmpInt = 0; tmpInt < x; tmpInt++)//build up the first variable array (numbers)
            {
                numbers[tmpInt] = tmpInt;
            }
            Array.Resize(ref numbers,y);// resize to variable input
            dynamicArray1 = numbers;//set the empty set array to the numbers array size 
            for (int z = 0; z < y; z++)//print to the new resize
            {
                Console.WriteLine(numbers[z].ToString());//print the numbers value
                Console.WriteLine(dynamicArray1[z].ToString());//print the empty set value
            }
        }
        Console.Write("Dynamic Arrays  ");
        var name = Console.ReadLine();

    }
}

Comments

0

Actually you can have Dynamic Arrays in C# it's very simple. keep in mind that the response to your question above is also correct you could declare a List Generic The way to Create a Dynamic Array would be to declare your Array for example

        string[] dynamicArry1 = { };//notice I did not declare a size for the array
        List<String> tmpList = new List<string>();
        int i = 1;
        for(int tmpInt = 0; tmpInt < 5; tmpInt++)
        {
           tmpList.Add("Testing, 1.0." + tmpInt + ", 200, 3.4" + tmpInt +"," + DateTime.Now.ToShortDateString());
           //dynamicArry1[tmpInt] = new string[] { tmpList[tmpInt].ToCharArray() };
        }
        dynamicArry1 = tmpList.ToArray();

Comments

0

how about ArrayList ?

If I'm not wrong ArrayList is an implementation of dynamic arrays

Comments

0

Example of Defining Dynamic Array in C#:

        Console.WriteLine("Define Array Size? ");
        int number = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter numbers:\n");
        int[] arr = new int[number];

        for (int i = 0; i < number; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());
        }

        for (int i = 0; i < arr.Length; i++ )
        {
            Console.WriteLine("Array Index: "+i + " AND Array Item: " + arr[i].ToString());
        }

        Console.ReadKey();  

Comments

-1

like so

int nSize = 17;
int[] arrn = new int[nSize];

nSize++;
arrn = new int[nSize];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.