-3

Why the arr[1] value is changing when changing the value of arr1[1]? and similarly why it is changing when ((int[])o)[1] = 1000;

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[2];
        arr[1] = 10;
        Object o = arr;
        int[] arr1 = (int[])o;
        arr1[1] = 100;
        Console.WriteLine(arr[1]);
        ((int[])o)[1] = 1000;
        Console.WriteLine(arr[1]);
    }
}

Why the answer is 100 and 1000?

This code is question-5 from https://www.techbeamers.com/csharp-coding-interview-questions-developers/

0

2 Answers 2

1

Why the arr[1] value is changing when changing the value of arr1[1]?

This is your first question, but keep it now after answering the second.

and similarly why it is changing when ((int[])o)[1] = 1000;

This is your second question. Answer is by seeing what is the initial value of o variable.

Object o = arr;

The previous line sets o to arr, but it's not copying the elements from arr to o. They're reference types, so, now both o and arr refers to the same memory block. Therefore, any changes made to arr will affect o and vice-versa. Because they're sharing the same memory.

Now, let's get back to your first question:

It's really the same answer as the second one.

int[] arr1 = (int[])o;

The previous line sets arr1 to o, but again, they just holds the same memory address.

To summarize:

  • You created arr
  • You created o and made its reference the same as arr, so they both share the same memory.
  • You created arr1 and made its reference the same as o which also same as arr. so, arr and arr1 and o, all have the same reference.

Any change to any of them will affect the others.

Sign up to request clarification or add additional context in comments.

3 Comments

"Why ? Because you changed it!" ... That doesn't help to explain to OP why changing the value in the second array also causes the value to change in first array. I assume you noticed these are two different arrays?
@ADyson, I didn't notice that one of them is arr and the other is arr1, thought there is only arr and o. but the same concept explained in the second part of the current answer still holds for the first part. But I'm going to edit my answer anyways.
Thanks for your answer. It helped me understand the code
0

arr, arr1 and o point to the same base memory address of the same array.

In C# an array is used by reference.

Reference is a hidden managed pointer to forget to manage it.

2 Comments

To make it more clear (I hope). You created a new array, and made the variable arr reference it. Then you set the second element in that array to 10 through the arrow variable. Then you set the variable o to reference the same array (that o is of type object doesn't really matter; variables of type object can refer to anything). Then you set variable arr1 to refer to the same array. At this point, arr, o and arr1 all refer to the same thing, that array you created; though access to the array through o requires casting it back to an array reference.
That's it. Casting is needed to match the type of the thing referenced, the array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.