---
title: "Automatically Copy one to array to another array"  
description: "Automatically Copy one to array to another array"  
author: "Anonymous User"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1600/automatically-copy-one-to-array-to-another-array  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Automatically Copy one to array to another array

I wrote this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that fills an [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) and a [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) array copies the first. I change some numbers in the second array, but when I [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) both [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations), the first one has also changed. This is my code:

```
       int[] array1 = new int[5];
        int[] array2 = new int[5];
        int temp;
        for (int i = 0; i < array1.Length; i++)
        {
            array1[i] = i;
        }
        array2 = array1;
        temp = array2[2];
        array2[2] = array2[4];
        array2[4] = temp;
        for (int i = 0; i < array1.Length; i++)
        {
            richTextBox1.Text += array1[i].ToString() + " ";
        }
        for (int i = 0; i < array2.Length; i++)
        {
            richTextBox1.Text += array2[i].ToString() + " ";
        }
```

Can someone [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) to me why the output is 0 1 4 3 2 0 1 4 3 2 [instead of](https://www.mindstick.com/articles/330/triggers-in-sql-server) 0 1 2 3 4 0 1 4 3 2?

## Replies

### Reply by Anonymous User

```
array2 = array1;
```

You now have two variables that refer to the same array


---

Original Source: https://www.mindstick.com/forum/1600/automatically-copy-one-to-array-to-another-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
