Can you initialize an array without specifying size ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you initialize an array without specifying size ?

1st Oct 2017, 6:09 AM
Ajith John Varghese
Ajith John Varghese - avatar
7 Answers
+ 1
k.
1st Oct 2017, 6:11 AM
Ajith John Varghese
Ajith John Varghese - avatar
+ 1
In a list you do not need to predefine the size, you just can add to is. Do you know yhe size on initialisation ? list<int> mylist = new list <int>; https://www.dotnetperls.com/list
1st Oct 2017, 6:57 AM
sneeze
sneeze - avatar
+ 1
I meant it is known then ( when you use it ). It is of course runtime allocation.
1st Oct 2017, 6:59 AM
Ajith John Varghese
Ajith John Varghese - avatar
0
Can you use a list ?
1st Oct 2017, 6:53 AM
sneeze
sneeze - avatar
0
yes. since its size is predefined
1st Oct 2017, 6:55 AM
Ajith John Varghese
Ajith John Varghese - avatar
0
True
1st Oct 2017, 7:02 AM
sneeze
sneeze - avatar
0
Definitely use a List<T> any time you want to add/remove data, since resizing arrays is expensive. If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful. List<T> offers a lot more functionality than an array (although LINQ evens it up a bit), and is almost always the right choice. More on C# List : http://csharp.net-informations.com/collection/list.htm
29th Jun 2020, 7:00 AM
rahul kumar
rahul kumar - avatar