c# - How to copy elements in one array to another array

Copy elements from one Array to another Array
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can copy elements from one Array to another Array. That means we will copy all the elements from one Array to another Array. In this .net c# example code, we will use the Array class CopyTo() method to copy the elements from one Array instance to another Array instance.

In the beginning, we have to initialize a new empty Array instance with the same size and the same data type as the source Array. This is the Array instance where we will put the elements that we will copy from the source Array instance. Then we will call the Array class CopyTo() method to copy the source Array elements to the newly initialized Array instance.

The Array CopyTo() method copies all the elements of the current one-dimensional array to the specified one-dimensional array. The Array CopyTo(Array, Int32) method overload copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. The index is specified as a 32-bit integer. So using this method .net developers can copy elements from one Array to another Array instance.

The Array CopyTo(Array array, int index) method has two parameters those are array and index. The array parameter is the one-dimensional array that is the destination of the elements copied from the current array. And the index parameter is a 32-bit integer that represents the index in the array at which copying begins. So here we will pass the newly created Array instance for the array parameter value and 0 for the index parameter value to copy source array elements to the destination Array instance.

The CopyTo(Array array, int index) method throws several exceptions. Such as it throws ArgumentNullException if the source array is null. It also throws ArgumentOutOfRangeException if the index is less than the lower bound of the array. It throws RankException if the source array is multidimensional and throws InvalidCastException if at least one element in the source Array cannot be cast to the type of destination array.
array-copy.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        string[] birds = new string[]
        {
            "Great Jacamar",
            "Noisy Pitta",
            "Velvet Asity"
        };

        string[] birds2 = new string[birds.Length];
        birds.CopyTo(birds2, 0);

        Label1.Text = "birds array.........<br />";
        foreach (string s in birds)
        {
            Label1.Text += s + "<br />";
        }

        Label1.Text += "<br />birds2 array[after copy].........<br />";
        foreach (string s in birds2)
        {
            Label1.Text += s + "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - array copy</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - array copy
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="X-Large"  
            >  
        </asp:Label>  
        <br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="copy array"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples