c sharp

How To Copy Array Elements to New Array in C#

In C#, arrays are commonly used to store collections of elements, and there are times when you may need to copy the elements from one array to another to perform operations or manipulate the data separately. This article will discuss various methods for copying array elements to a new array in C#.

How To Copy Array Elements to New Array in C#

Copying array elements to a new array in C# is significant as it allows you to preserve the original data while working with a separate set of elements, enabling independent modifications or operations without affecting the original array. This approach ensures data integrity, facilitates data manipulation, and provides flexibility in handling and processing array elements in various scenarios, below are some ways to do it:

1: Using a Loop

One of the simplest ways to copy array elements is by using a loop, such as the for loop or foreach loop. Here’s an example:

using System;

class Copy_elements
{
    static void Main()
    {
        int[] sourceArray = { 1, 2, 3, 4, 5 };
        int[] destinationArray = new int[sourceArray.Length];
        for (int i = 0; i < sourceArray.Length; i++)
        {
            destinationArray[i] = sourceArray[i];
        }
        Console.WriteLine("Source Array: " + string.Join(", ", sourceArray));
        Console.WriteLine("Destination Array: " + string.Join(", ", destinationArray));
    }
}

First, the code includes the necessary using System directive to access the console class and related functionalities.

Inside the Main(), an integer array called sourceArray is declared and initialized with the values { 1, 2, 3, 4, 5 }.

A new integer array called destinationArray is created with the same length as the sourceArray using the new keyword. Then, each component of the sourceArray is iterated by using a loop, and in each iteration, the current element from the sourceArray is assigned to the corresponding index in the destinationArray.

2: Using Array.Copy()

C# provides a built-in method called Array.Copy() that allows for efficient array element copying. Here’s an example:

using System;

class Program
{
    static void Main()
    {
        int[] sourceArray = { 1, 2, 3, 4, 5 };
        int[] destinationArray = new int[sourceArray.Length];
        Array.Copy(sourceArray, destinationArray, sourceArray.Length);
        Console.WriteLine("Source Array: " + string.Join(", ", sourceArray));
        Console.WriteLine("Destination Array: " + string.Join(", ", destinationArray));
    }
}

First, the code includes the necessary using System directive to access the console class and related functionalities. Inside the Main(), an integer array called sourceArray is declared and initialized with the values { 1, 2, 3, 4, 5 }.

A new integer array called destinationArray is created with the same length as the sourceArray using the new keyword.

Then, the Array.Copy() method is used to copy elements from the sourceArray to the destinationArray. The first argument is the source array, the second argument is the destination array, and the third argument is the length of the elements to be copied.

3: Using Array.Clone()

Another built-in approach is to use the Array.Clone() method, which creates a shallow copy of the array. Here’s an example:

using System;

class Program
{
    static void Main()
    {
        int[] sourceArray = { 1, 2, 3, 4, 5 };
        int[] destinationArray = (int[])sourceArray.Clone();
        Console.WriteLine("Source Array: " + string.Join(", ", sourceArray));
        Console.WriteLine("Destination Array: " + string.Join(", ", destinationArray));
    }
}

First, the code includes the necessary using System directive to access the Console class and related functionalities.

Inside the Main method, an integer array called sourceArray is declared and initialized with the values { 1, 2, 3, 4, 5 }.

Then, a new integer array called destinationArray is created using (int[])sourceArray.Clone(). By using the Clone method, which generates a new array with the same length and number of items as the original sourceArray, this executes a shallow duplicate of the sourceArray.

Conclusion

Copying array elements to a new array is a common operation in C# programming. You can do it in two ways that are using the Array.Copy() method, or the Array.Clone() method. This tutorial provides example programs to elaborate on copying elements to a new array.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.