0

In C# I have three arrays, string[] array1, 2 and 3 and they all have differnt values. I would love to do what I can do in php which is:

$array = array(); $array[] .= 'some value';

Whats the equivalent way of doing this in C#?

TheWebs
  • 10,087
  • 21
  • 77
  • 164
  • 1
    It is better to work with [Lists](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) if you want to change the size easily. [This might be worth a gander too](http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which) – musefan Apr 19 '13 at 16:54
  • [This](http://stackoverflow.com/questions/1547252/how-do-i-concatenate-two-arrays-in-c), [this](http://stackoverflow.com/questions/2788636/array-concatenation-in-c-sharp) and [this](http://stackoverflow.com/questions/304816/most-efficient-way-to-append-arrays-in-c) – Zbigniew Apr 19 '13 at 16:56

6 Answers6

7

In C#, you'd typically use a List<string> instead of string[].

This will allow you to write list.Add("some value") and will "grow" the list dynamically.

Note that it's easy to convert between a list and an array if needed. List<T> has a constructor that takes any IEnumerable<T>, including an array, so you can make a list from an array via:

var list = new List<string>(stringArray);

You can convert a list to an array via:

var array = list.ToArray();

This is only required if you need an array, however (such as working with a third party API). If you know you're going to work with collections that vary in size, it's often better to just always stick to List<T> and not use arrays.

Reed Copsey
  • 522,342
  • 70
  • 1,092
  • 1,340
  • 1
    With a comment that if you do need to work with arrays (if, for instance, you're getting arrays from some third party library, and need to send back the result to that library as an array), the List constructor can take an array, as can list.AddRange, and when you're done, you can call list.ToArray(); – neminem Apr 19 '13 at 16:58
  • @neminem True. Most of the time, when changing languages, that is less of an issue. It's usually more a matter of learning how each language and framework handles the basics, but yes, converting between the two is fairly simple. – Reed Copsey Apr 19 '13 at 17:02
2

You can create a list and add the array values to it and then convert that list back to array.

int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };

// Create new List of integers and call AddRange twice
var list = new List<int>();
list.AddRange(array1);
list.AddRange(array2);

// Call ToArray to convert List to array
int[] array3 = list.ToArray();
Kurubaran
  • 7,956
  • 5
  • 36
  • 62
0

You could use dinamic lists List<string>. You can do

List<string> TotalList = array1.ToList();

Then you can TotalList.AddRange(array2) and so on....

Daniel Möller
  • 74,597
  • 15
  • 158
  • 180
0

if you simply want to merge your arrays

use linq .Concat

 array1 = array1.Concat(array2).Concat(array3).ToArray();
Community
  • 1
  • 1
Manish Mishra
  • 11,383
  • 5
  • 23
  • 52
0

List<T> or LINQ may be the easiest solutions, but you can also do it the old fashioned way:

// b1 is now 5 bytes
byte[] b1 = Get5BytesFromSomewhere();

// creating a 6-byte array
byte[] temp = new byte[6];

// copying bytes 0-4 from b1 to temp
Array.copy(b1, 0, temp, 0, 5);

// adding a 6th byte
temp[5] = (byte)11;

// reassigning that temp array back to the b1 variable
b1 = temp;
Joe Enos
  • 36,707
  • 11
  • 72
  • 128
0

Easy with linq:

int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
int[] array3 = { 3, 4 ,5, 9, 10 };

var result = array1
    .Concat(array2)
    .Concat(array3)
    .ToArray();
Dave Bish
  • 17,987
  • 6
  • 40
  • 60