0

I am new to C# programming, I migrated from python. I want to append two or more array (exact number is not known , depends on db entry) into a single array like the list.append method in python does. Here the code example of what I want to do

int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = {7,8,9};
int[] d;

I don't want to add all the arrays at a time. I need somewhat like this

// I know this not correct
d += a;
d += b;
d += c;

And this is the final result I want

d = {{1,2,3},{4,5,6},{7,8,9}};

it would be too easy for you guys but then I am just starting with c#.

4
  • d = {{1,2,3},{4,5,6},{7,8,9}}; is not int[] Commented Jan 30, 2017 at 11:23
  • Shall d be a jagged array, i.e. array of array {{1,2,3},{4,5,6},{7,8,9}} or just a simple 1D one: {1, 2, 3, 4, 5, 6, 7, 8, 9}? Commented Jan 30, 2017 at 11:23
  • @Roma I dont want it in one step Commented Jan 30, 2017 at 11:28
  • @Dmitry it should be jagged i.e. array of array like code {{1,2,3},{4,5,6},{7,8,9}} Commented Jan 30, 2017 at 11:31

4 Answers 4

2

Well, if you want a simple 1D array, try SelectMany:

  int[] a = { 1, 2, 3 };
  int[] b = { 4, 5, 6 };
  int[] c = { 7, 8, 9 };

  // d == {1, 2, 3, 4, 5, 6, 7, 8, 9}
  int[] d = new[] { a, b, c } // initial jagged array
    .SelectMany(item => item) // flattened
    .ToArray();               // materialized as a array

if you want a jagged array (array of arrays)

  // d == {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
  // notice the declaration - int[][] - array of arrays of int
  int[][] d = new[] { a, b, c };

In case you want to append arrays conditionally, not in one go, array is not a collection type to choose for d; List<int> or List<int[]> will serve better:

  // 1d array emulation
  List<int> d = new List<int>();
  ...
  d.AddRange(a);
  d.AddRange(b);
  d.AddRange(c); 

Or

  // jagged array emulation
  List<int[]> d = new List<int[]>();
  ...
  d.Add(a); //d.Add(a.ToArray()); if you want a copy of a  
  d.Add(b);
  d.Add(c);
Sign up to request clarification or add additional context in comments.

3 Comments

It looks like the last of those (List<int[]>) is what the OP needs.
sir it gives me an error error CS0246: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) is it in System namespace?
no, List<T> is in the using System.Collections.Generic; namespace
0

It seems from your code that d is not a single-dimensional array, but it seems to be a jagged array (and array of arrays). If so, you can write this:

int[][] d = new int[][] { a, b, c };

If you instead want to concatenate all arrays to a new d, you can use:

int[] d = a.Concat(b).Concat(c).ToArray();

3 Comments

Sir I don't need a one liner imagine it in a for loop and one by one I want to add it
it is 1D array.
So then use the first sample.
0
var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);

Comments

0

You can use Array.Copy. It copies a range of elements in one Array to another array. Reference

int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = {7,8,9};

int[] combined = new int[a.Length + b.Length + c.Length];
Array.Copy(a, combined, a.Length);
Array.Copy(b, 0, combined, a.Length, b.Length);
Array.Copy(c, 0, combined, a.Length, b.Length, c.Length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.