-1

How I can copy a data from array to another array in MVC C# without overwrite it.

string[] total = new {"Apple", "Banana", "Cat"};
string[] arrayA= new {"Donkey", "Ear", "Frog"};
total= new List<string>().Concat(arrayA).ToArray();

This code basically just Copy the data from arrayA and overwrite it. So is there anyway to add or copy the data from arrayA and add it into total After string[] total = new List<string>().Concat(arrayA).ToArray(); my total value had become arrayA and the value inside total is been overwrite.

  • You mean like `string[] total = arrayA.ToArray();`? – ProgrammingLlama Feb 18 '21 at 07:41
  • @John not my data inside ```total``` is still been overwrite by ```arrayA``` – SdoAna GnGmnk Feb 18 '21 at 07:46
  • What do you mean "overwritten"? `total` didn't exist before you assigned a value to it. By definition of not existing, it can't be overwritten. – ProgrammingLlama Feb 18 '21 at 07:47
  • 1
    hmm.. it's somewhat unclear what you're asking. But it seems your snippet does not reflect your actual issue or concern here. – Brett Caswell Feb 18 '21 at 07:47
  • OP: Please edit your question to be clear. – ProgrammingLlama Feb 18 '21 at 07:53
  • Are you looking for deep copy? – Athanasios Kataras Feb 18 '21 at 07:53
  • 1
    @AthanasiosKataras But OP is working with an array of strings. – ProgrammingLlama Feb 18 '21 at 07:54
  • True, so it's an irrelevant point. – Athanasios Kataras Feb 18 '21 at 07:54
  • OP: Are you asking how to make an array of the same length as `arrayA` but without values for the items (i.e. all the items would be `null`)? – ProgrammingLlama Feb 18 '21 at 07:55
  • I think this question may be an [XY Problem](https://meta.stackexchange.com/a/66378/226912) @SdoAnaGnGmnk would it be fair to presume that you're intending to 'bind' to `total` in some way, and are seeing issues in that effort? – Brett Caswell Feb 18 '21 at 08:09
  • @BrettCaswell I wanted to let ```total``` to also have the data inside ```arrayA``` – SdoAna GnGmnk Feb 18 '21 at 08:11
  • Note that the second `string[] total = ` is invalid because you can't redeclare the `total` variable. That should simply be `label = ` to assign a new value to the `label` variable. – ProgrammingLlama Feb 18 '21 at 08:12
  • @John there is a typo it should be ```total= new List().Concat(arrayA).ToArray();``` – SdoAna GnGmnk Feb 18 '21 at 08:14
  • 1
    "I wanted to let `total` to also have the data inside `arrayA`" I see, well the initial snippet you provided did that already, but your updated snippet is clearer on what the issue was. My misunderstanding here was presuming that since you were using `Concat` you knew (or had looked up) its use, and that your actual issue related to something in regards to aspnet mvc. Having said that, aspnet and mvc aren't attributable or relevant to this question - should probably remove those tags and references to them in the description. – Brett Caswell Feb 18 '21 at 08:51

3 Answers3

1
  1. Array CopyTo

In terms of copying data you can do this with CopyTo solution like above:

var source = new string[] { "1", "2", "3" };
var target = new string[3];

try
{
  source.CopyTo(target, 0);
}
catch (InvalidCastException)
{
  foreach (var element in target)
  {
    Console.WriteLine(element);
  }
}
  1. Conversion array to list

If you want to append an array you can't do this in the proper way (without setting the max number/length of target array and waste memory).List is much better option here (conversion of array to a list is might what you searching for).

List<int> lst = ints.OfType<string>().ToList();
  1. Array Cloning

Another option would be Cloning the whole array which meaning that shallow copy is performed over array items.

var source = new[] { "aa", "bb", "cc" };
var target = (string[])source.Clone();
NSKBpro
  • 373
  • 1
  • 14
1

You should make total a list because you want to extend it with new entries:

List<string> total = new List<string> { "Apple", "Banana", "Cat" };

And then you can use AddRange to add all of the values from arrayA:

total.AddRange(arrayA);

If you really need an array then this should work, though the List option is still better:

string[] total = new {"Apple", "Banana", "Cat"};
string[] arrayA = new {"Donkey", "Ear", "Frog"};
total = total.Concat(arrayA).ToArray();

There is also an 'Array.Resize' method. Ultimately this just creates a new array and copies the data so it isn't really much better than the LINQ method above:

string[] total = new {"Apple", "Banana", "Cat"};
string[] arrayA = new {"Donkey", "Ear", "Frog"};
int initialTotalLength = total.Length;
// resize the array
Array.Resize(ref total, total.Length + arrayA.Length);
// append the new entries to the "empty" positions in the array
arrayA.CopyTo(total, initialTotalLength);
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Generally agreeable answer as-is. In regards to 'overwrite' though (which is still unclear actually), I presume OP means 'assigned' or 'assignment'. In which case, could be interested in a means to resize\redimension\concat referentially without an assignment. So, A helper or extension method (perhaps?) could be part of this solution\answer - if that was actually a concern. – Brett Caswell Feb 18 '21 at 08:25
  • 1
    @Brett Yeah, I'm not 100% to be honest. I think they just meant that they wanted to combine the two arrays in the `total` array. I hope this answers any slight variation on that question that they might have. – ProgrammingLlama Feb 18 '21 at 08:33
0

You will have to create another variable and store combine array over there var arrayA = [1, 2]; var arrayB = [3, 4];

var newArray = arrayA.concat(arrayB);

The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).

using reference https://stackoverflow.com/a/4156145/3510852

firoz khan
  • 27
  • 7