合并数组或追加到数组

时间:2022-03-29 21:14:55

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:

在C#中我有三个数组,string [] array1,2和3,它们都有不同的值。我很乐意在php中做我能做的事情:

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

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

Whats the equivalent way of doing this in C#?

什么是在C#中这样做的相同方式?

6 个解决方案

#1


7  

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

在C#中,您通常使用List 而不是string []。

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

这将允许您编写list.Add(“some value”)并动态“增长”列表。

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:

请注意,如果需要,可以轻松地在列表和数组之间进行转换。 List 有一个构造函数,它接受任何IEnumerable ,包括一个数组,因此您可以通过以下方式从数组中创建一个列表:

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.

只有在需要阵列时才需要这样做(例如使用第三方API)。如果您知道自己将使用不同大小的集合,那么通常最好始终坚持List 而不是使用数组。

#2


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();

#3


0  

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

您可以使用dinamic列表List 。你可以做

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

List TotalList = array1.ToList();

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

然后你可以TotalList.AddRange(array2)等等....

#4


0  

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

List 或LINQ可能是最简单的解决方案,但您也可以采用老式的方式:

// 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;

#5


0  

if you simply want to merge your arrays

如果你只是想合并你的数组

use linq .Concat

使用linq .Concat

 array1 = array1.Concat(array2).Concat(array3).ToArray();

#6


0  

Easy with linq:

简单的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();

#1


7  

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

在C#中,您通常使用List 而不是string []。

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

这将允许您编写list.Add(“some value”)并动态“增长”列表。

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:

请注意,如果需要,可以轻松地在列表和数组之间进行转换。 List 有一个构造函数,它接受任何IEnumerable ,包括一个数组,因此您可以通过以下方式从数组中创建一个列表:

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.

只有在需要阵列时才需要这样做(例如使用第三方API)。如果您知道自己将使用不同大小的集合,那么通常最好始终坚持List 而不是使用数组。

#2


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();

#3


0  

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

您可以使用dinamic列表List 。你可以做

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

List TotalList = array1.ToList();

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

然后你可以TotalList.AddRange(array2)等等....

#4


0  

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

List 或LINQ可能是最简单的解决方案,但您也可以采用老式的方式:

// 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;

#5


0  

if you simply want to merge your arrays

如果你只是想合并你的数组

use linq .Concat

使用linq .Concat

 array1 = array1.Concat(array2).Concat(array3).ToArray();

#6


0  

Easy with linq:

简单的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();