merge array c# code example
Example 1: c# merging two arrays
T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, newArray, array1.Length);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);
Example 2: c# add 2 arrays
public static int[] AddArrays(int[] a, int[] b)
{
int[] newArray = new int[a.Length];
for (int i = 0; i<a.Length; i++)
{
newArray[i]=a[i]+b[i];
}
return newArray;
}
Example 3: c# merge two lists
public class A { int Id { get; set; } A() { } A(int id) { Id = id;} }
public class B { int Id { get; set; } B() { } B(int id) { Id = id;} }
List<A> list = new List<A>() { new A( Id = 1 ), new A( Id = 2 ) };
List<B> list1 = new List<B>() { new B( Id = 3 ), new B( Id = 4 ) };
List<object> all = (from x in list select (object)x).ToList();
all.AddRange((from x in list1 select (object)x).ToList());
foreach (object item in all)
{
bool obj1 = item is A;
Console.WriteLine(obj1 ? (item as A).Id : (item as B).Id);
}