C# Program to copy all elements of an array to another array


In this program, You will learn how to copy all elements of an array to another array in C#.


arr2[i] = arr1[i]

Example: How to copy all elements of an array to another array in C#.

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int i;
		int[] arr1 = new int[5];
		int[] arr2 = new int[5];

		Console.Write("Enter five numbers:");
		for (i = 0; i < arr1.Length; i++)
		{
			arr1[i] = Convert.ToInt32(Console.ReadLine());
		}

		/* copy start here */
		for (i = 0; i < arr1.Length; i++)
		{
			arr2[i] = arr1[i];
		}

		Console.Write("New Array List:");
		for (i = 0; i < arr2.Length; i++)
		{
			Console.Write(arr2[i] + " ");
		}
	}
}

Output:

Enter five numbers:10
20
30
40
50
New Array List:10 20 30 40 50