Home > other >  Arrays.copyTo() from Java in C#
Arrays.copyTo() from Java in C#

Time:10-14

There is a function in Java "Arrays.copyTo()" and I need to find a method that works the same way as Arrays.copyTo() in C#. How would I do this?

CodePudding user response:

string[] arr = new string[n];
string[] newArray = new string[m];
Array.Copy(arr, newArray , length);

So this is for C#

Array.Copy has four different overloads

CodePudding user response:

There are two examples of this on Microsoft's own site. Array.Copy MethodArray.Copy Method or Array.Copy Method

for example you first create two new array

public class Program
{
    public static void Main()
    {
        var source = new[] { "Ally", "Bishop", "Billy" };
        var target = new string[4];
        
        target.SetValue("onur",0);
        target.SetValue("veli",1);
        target.SetValue("deli",2);
        source.CopyTo(target, 1);
        foreach (var item in target)
        {
          Console.WriteLine(item);
        }
    
    }
}

or

var source = new[] { "Ally", "Bishop", "Billy" };
var target = new[] { "Veli", "Deli", "lly", "Kel" };

Array.Copy(source, target,1);
foreach (var item in target)
{
  Console.WriteLine(item);
}
  • Related