How to copy array section to another in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[6];

            arr[0] = 1;
            arr[1] = 2;
            arr[2] = 3;
            arr[3] = 4;
            arr[4] = 5;
            arr[5] = 6;

            int[] arr_copy = new int[4];

            Array.Copy(arr, 0, arr_copy, 0, 4);

            for (int i = 0; i < arr_copy.Length; i++)
                Console.WriteLine(arr_copy[i]);
        }
    }
}

/*
run:

1
2
3
4

*/

 



answered Jan 6, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] arr = new char[6];

            arr[0] = 'a';
            arr[1] = 'b';
            arr[2] = 'c';
            arr[3] = 'd';
            arr[4] = 'e';
            arr[5] = 'f';

            char[] arr_copy = new char[6];

            Array.Copy(arr, 1, arr_copy, 0, 3);

            for (int i = 0; i < arr_copy.Length; i++)
                Console.WriteLine(arr_copy[i]);
        }
    }
}

/*
run:

b
c
d

*/

 



answered Jan 6, 2017 by avibootz

Related questions

...