0
0
C Sharp (C#)programming~5 mins

Array as reference type behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Arrays in C# are special because they are stored as references. This means when you change an array through one variable, other variables pointing to the same array see the change too.

When you want to share a list of items between different parts of your program without copying it.
When you want to understand why changing an array in one place affects it in another.
When you want to pass an array to a method and have the method modify the original array.
When you want to avoid making extra copies of large arrays to save memory.
Syntax
C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        int[] originalArray = {1, 2, 3};
        int[] referenceToArray = originalArray; // Both variables point to the same array

        referenceToArray[0] = 10; // Change affects originalArray too

        Console.WriteLine(originalArray[0]); // Prints 10
    }
}

Arrays are reference types in C#, so variables hold references (addresses) to the actual array data.

Assigning one array variable to another copies the reference, not the array contents.

Examples
Shows that assigning a new array to the second variable breaks the link to the original array.
C Sharp (C#)
int[] emptyArray = new int[0];
int[] anotherReference = emptyArray;
anotherReference = new int[5]; // Now points to a new array, original emptyArray unchanged
Even with one element, changing through one reference changes the original array.
C Sharp (C#)
int[] singleElementArray = {5};
int[] referenceCopy = singleElementArray;
referenceCopy[0] = 20;
// singleElementArray[0] is now 20 too
Changing the last element through the reference affects the original array.
C Sharp (C#)
int[] array = {1, 2, 3};
int[] reference = array;
reference[2] = 99;
// array[2] is now 99
Sample Program

This program shows that changing the array through one variable changes it for the other because both point to the same array.

C Sharp (C#)
using System;

class Program
{
    static void PrintArray(string label, int[] array)
    {
        Console.Write(label + ": ");
        foreach (int item in array)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }

    static void Main()
    {
        int[] originalArray = {1, 2, 3};
        Console.WriteLine("Before change:");
        PrintArray("originalArray", originalArray);

        int[] referenceToArray = originalArray; // reference copy
        referenceToArray[1] = 20; // change through reference

        Console.WriteLine("After change through referenceToArray:");
        PrintArray("originalArray", originalArray);
        PrintArray("referenceToArray", referenceToArray);
    }
}
OutputSuccess
Important Notes

Time complexity: Access and modification by index is O(1).

Space complexity: No extra space used when copying references.

Common mistake: Expecting that assigning one array variable to another copies the array contents, but it only copies the reference.

Use this behavior when you want multiple parts of your program to work on the same array data without copying.

Summary

Arrays in C# are reference types, so variables hold references to the actual array.

Assigning one array variable to another copies the reference, not the array itself.

Changing the array through any reference changes the original array data.