Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Array Copying and Memory | Array Manipulation and Sorting
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookArray Copying and Memory

When you work with arrays in C#, it's important to understand how they are stored in memory and what happens when you copy or assign them. Arrays in C# are reference types, meaning that variables hold a reference (or pointer) to where the array's data is stored in memory, not the actual data itself. This has significant implications when you assign one array variable to another or when you want to make a true copy of an array's contents. If you overlook these details, you might accidentally change data in places you didn't intend, leading to bugs that are hard to track down.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] original = { 1, 2, 3, 4, 5 }; // Assigning the array reference int[] assigned = original; // Copying the array elements int[] copied = new int[original.Length]; for (int i = 0; i < original.Length; i++) { copied[i] = original[i]; } // Modify the assigned array assigned[0] = 99; // Modify the copied array copied[1] = 77; System.Console.WriteLine("original: " + string.Join(", ", original)); System.Console.WriteLine("assigned: " + string.Join(", ", assigned)); System.Console.WriteLine("copied: " + string.Join(", ", copied)); } } }

When you assign one array variable to another, both variables point to the same memory location. This means that a change made through one variable appears in the other, because they are just two references to the same array. If you want to avoid this, you must copy each element into a new array, as shown in the code above. This creates a new array in memory, so changes to one array do not affect the other.

This brings up the concepts of shallow copy and deep copy. A shallow copy copies only the reference to the array, not the actual elements. A deep copy creates a new array and copies each element into it, resulting in two independent arrays.

Program.cs

Program.cs

copy
1234567891011121314151617
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 20, 30 }; int[] shallow = numbers; shallow[1] = 99; System.Console.WriteLine("numbers: " + string.Join(", ", numbers)); System.Console.WriteLine("shallow: " + string.Join(", ", shallow)); } } }

The code sample demonstrates what happens when you perform a shallow copy of an array in C#. When you assign one array variable to another, such as int[] shallow = numbers;, you are not creating a new array or copying the elements. Instead, both variables, numbers and shallow, point to the same array in memory. This means that any change you make through one variable will be visible through the other.

Note
Definition

A shallow copy copies the reference to the array, so both variables point to the same memory location. A deep copy creates a new array and copies all elements, so the arrays are independent.

1. What happens when you assign one array variable to another in C#?

2. How can you create a true copy of an array?

3. Why is understanding array memory important when manipulating data?

question mark

What happens when you assign one array variable to another in C#?

Select the correct answer

question mark

How can you create a true copy of an array?

Select the correct answer

question mark

Why is understanding array memory important when manipulating data?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookArray Copying and Memory

Stryg for at vise menuen

When you work with arrays in C#, it's important to understand how they are stored in memory and what happens when you copy or assign them. Arrays in C# are reference types, meaning that variables hold a reference (or pointer) to where the array's data is stored in memory, not the actual data itself. This has significant implications when you assign one array variable to another or when you want to make a true copy of an array's contents. If you overlook these details, you might accidentally change data in places you didn't intend, leading to bugs that are hard to track down.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] original = { 1, 2, 3, 4, 5 }; // Assigning the array reference int[] assigned = original; // Copying the array elements int[] copied = new int[original.Length]; for (int i = 0; i < original.Length; i++) { copied[i] = original[i]; } // Modify the assigned array assigned[0] = 99; // Modify the copied array copied[1] = 77; System.Console.WriteLine("original: " + string.Join(", ", original)); System.Console.WriteLine("assigned: " + string.Join(", ", assigned)); System.Console.WriteLine("copied: " + string.Join(", ", copied)); } } }

When you assign one array variable to another, both variables point to the same memory location. This means that a change made through one variable appears in the other, because they are just two references to the same array. If you want to avoid this, you must copy each element into a new array, as shown in the code above. This creates a new array in memory, so changes to one array do not affect the other.

This brings up the concepts of shallow copy and deep copy. A shallow copy copies only the reference to the array, not the actual elements. A deep copy creates a new array and copies each element into it, resulting in two independent arrays.

Program.cs

Program.cs

copy
1234567891011121314151617
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 10, 20, 30 }; int[] shallow = numbers; shallow[1] = 99; System.Console.WriteLine("numbers: " + string.Join(", ", numbers)); System.Console.WriteLine("shallow: " + string.Join(", ", shallow)); } } }

The code sample demonstrates what happens when you perform a shallow copy of an array in C#. When you assign one array variable to another, such as int[] shallow = numbers;, you are not creating a new array or copying the elements. Instead, both variables, numbers and shallow, point to the same array in memory. This means that any change you make through one variable will be visible through the other.

Note
Definition

A shallow copy copies the reference to the array, so both variables point to the same memory location. A deep copy creates a new array and copies all elements, so the arrays are independent.

1. What happens when you assign one array variable to another in C#?

2. How can you create a true copy of an array?

3. Why is understanding array memory important when manipulating data?

question mark

What happens when you assign one array variable to another in C#?

Select the correct answer

question mark

How can you create a true copy of an array?

Select the correct answer

question mark

Why is understanding array memory important when manipulating data?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 5
some-alt