Dynamic array in C #

Is there a way to create a dynamic array in C #?

+95
c #
Feb 27 '09 at 13:58
source share
8 answers

Take a look at the General Lists .

+142
Feb 27 '09 at 13:59
source share

Turning around to answer Chris and Migol with a sample code.

Using array

Student[] array = new Student[2]; array[0] = new Student("bob"); array[1] = new Student("joe"); 

Using a shared list. Under the hood, a List <T> class uses an array to store, but does it in such a way that it can grow efficiently.

 List<Student> list = new List<Student>(); list.Add(new Student("bob")); list.Add(new Student("joe")); Student joe = list[1]; 
+77
Feb 27 '09 at 14:45
source share

Sometimes simple arrays are preferable to general lists, because they are more convenient (for example, higher performance for expensive computations -Numerical algebraic applications or for data exchange with statistics software such as R or Matlab)

In this case, you can use the ToArray () method after the list is run dynamically.

 List<string> list = new List<string>(); list.Add("one"); list.Add("two"); list.Add("three"); string[] array = list.ToArray(); 

Of course, this only makes sense if the size of the array is never known or fixed in advance. if you already know the size of your array at some point in the program, it is better to initiate it as an array of a fixed length. (If you, for example, retrieve data from a ResultSet, you can calculate their size and dynamically initiate an array of this size)

+45
Nov 02 2018-10-11T00:
source share

List<T> for strongly typed, or ArrayList if you have .NET 1.1 or love of variables.

+37
Feb 27 '09 at 2:00
source share

Use an array list that actually implements the array. First, an array of size 4 is required, and when it is filled, a new array is created with its double size, and the data of the first array is copied to the second array, now the new element is inserted into the new array. Also, the name of the second array creates the first alias, so that it can be accessed with the same name as the previous one, and the first array will be deleted

+1
Jun 24 '15 at 7:18
source share

This answer seems to be the answer you are looking for Why can't I do this: dynamic x = new ExpandoObject {Foo = 12, Bar = "twelve"}

Read about ExpandoObject here https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx

And the dynamic type is here https://msdn.microsoft.com/en-GB/library/dd264736.aspx

+1
Jul 14 '16 at 12:42 on
source share

Dynamic matrix example:

 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(); 
+1
Jul 31 '17 at 8:34
source share

You can do this with dynamic objects:

 var dynamicKeyValueArray = new[] { new {Key = "K1", Value = 10}, new {Key = "K2", Value = 5} }; foreach(var keyvalue in dynamicKeyValueArray) { Console.Log(keyvalue.Key); Console.Log(keyvalue.Value); } 
0
Nov 19 '18 at 5:09
source share



All Articles