Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:21:02+00:00 2026-06-05T06:21:02+00:00

I wrote this code that fills an array and a second array copies the

  • 0

I wrote this code that fills an array and a second array copies the first. I change some numbers in the second array, but when I output both arrays, 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 to me why the output is 0 1 4 3 2 0 1 4 3 2 instead of 0 1 2 3 4 0 1 4 3 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:21:03+00:00Added an answer on June 5, 2026 at 6:21 am

    Because arrays are reference types, assigning array2 = array1 causes the array2 variable to reference the same array as array1.

    Before assignment:

    array1  --->  [0, 1, 2, 3, 4]
    
    array2  --->  [0, 0, 0, 0, 0]
    

    After assignment:

    array1  --->  [0, 1, 2, 3, 4]
             /
    array2  -     [0, 0, 0, 0, 0] <-- no longer reachable and will be garbage collected
    

    To actually create a copy of an array, call Clone.

    array2 = (int[])array1.Clone();
    

    After assignment with Clone:

    array1  --->  [0, 1, 2, 3, 4]
    
    array2  -     [0, 0, 0, 0, 0] <-- no longer reachable and will be garbage collected
             \
              ->  [0, 1, 2, 3, 4]
    

    Notice that the [0, 0, 0, 0, 0] array is never actually used, so you don’t even need to allocate it.

    //int[] array2 = new int[5];  <-- delete this line
    int[] array2 = (int[])array1.Clone();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code that compiles on Solaris gcc it works fine too for
I wrote this piece of code that is supposed to redirect something written on
this is my code that I write it but I want to use LINQ
I wrote this code. The constructor works normally, but in the destructor I get
I am currently working with some code that my co-worker wrote. Here is a
I wrote this function but can't get on the problem that gives me 'segmentation
So I have some code that looks like this: int a[10]; a = arrayGen(a,9);
How to write this JavaScript code without eval? var typeOfString = eval(typeof + that.modules[modName].varName);
I'm trying to write a regular expression for html code that looks like this:
I was wondering how to better write code like this so that it leverages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.