3

I'm learning c#, with my primary language before now being php. I was wondering how (or if) you could create an empty array in c#.

In php, you can create an array, and then add any number of entries to it.

$multiples=array();
$multiples[] = 1;
$multiples[] = 2;
$multiples[] = 3;

In c#, I'm having trouble doing something similar:

int[] arraynums = new int[];
arraynums[] = 1;
arraynums[] = 2;
arraynums[] = 3;

Which gives the error "array creation must have array size or array initializer." If I don't know how many entries I want to make, how do I do this? Is there a way around this?

James G.
  • 2,852
  • 3
  • 28
  • 52
  • 3
    Arrays have a static size, use a list – Matthew Mcveigh Nov 01 '13 at 23:05
  • I'm not sure, but I think that C# as most programing languages doesn't allow to create arrays with dinamic size. – Epsil0neR Nov 01 '13 at 23:06
  • In PHP arrays are more like dictionaries in C#, they are not real old school arrays, but a dynamic object instead. On the other hand, C# arrays are static closer to what they used to be in C and C++. To mimic PHP's array use a list or a dictionary instead. [It seems to me a bit sad that PHP is teaching that arrays bend and twist] – Theraot Nov 01 '13 at 23:07

5 Answers5

5

If you don't know the size in advance, use a List<T> instead of an array. An array, in C#, is a fixed size, and you must specify the size when creating it.

var arrayNums = new List<int>();
arrayNums.Add(1);
arrayNums.Add(2);

Once you've added items, you can extract them by index, just like you would with an array:

int secondNumber = arrayNums[1];
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • In what situations would arrays be superior (I'm googling now, real quick)? – James G. Nov 01 '13 at 23:09
  • 1
    @JamesG. They're used much more rarely in C#. They're useful if you know, in advance, how many items you will have, and you know that it will never change. Otherwise, use a `List` or other collection type. – Reed Copsey Nov 01 '13 at 23:09
  • http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which Yep, that would be the largest difference I've found, so far. – James G. Nov 01 '13 at 23:10
2

c# arrays have a static size.

int[] arraynums = new int[3];

or

int[] arraynums = {1, 2, 3}

if you want to use dynamic sized array, you should use ArrayList, or List.

ILker Özcan
  • 346
  • 4
  • 11
1

I would recommend using a different collection such as a List<T> or a Dictionary<TKey, TValue>. Calling the collection in PHP an array is just a misnomer. An array is a continuous fixed size block of memory that contains only a single type and offers direct access by calculating the offset for a given index. The data type in PHP does none of these things.

Examples;

List<int> my_ints = new List<int>();
my_ints.Add(500);


Dictionary<string, int> ids = new Dictionary<string, int>();
ids.Add("Evan", 1);

int evansId = ids["Evan"];

Examples of when to use an array;

string[] lines = File.ReadAllLines(myPath);
for (int i = 0; i < lines.Length; i++)
    // i perform better than other collections here!
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
1

Newer way, since .NET 4.6 / Core 1.0, in case somebody hits this:

System.Array.Empty<T>() method.

This is more efficient if called multiple times, as it's backed by a single static readonly array generated at compile time.

https://learn.microsoft.com/en-us/dotnet/api/system.array.empty https://referencesource.microsoft.com/#mscorlib/system/array.cs,3079

M.Bearden
  • 435
  • 5
  • 13
0

Try this post: Dynamic array in C#. It has a couple of links in the first answer that show alternate ways of indexing data. In C#, there is no way of making dynamic arrays but those links show some workarounds.

Community
  • 1
  • 1