Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Accessing and Modifying Array Elements | Array Fundamentals
C# Arrays

bookAccessing and Modifying Array Elements

Arrays in C# are powerful tools for storing and organizing multiple values of the same type. To make the most of arrays, you need to understand how to access and change their elements. C# arrays use zero-based indexing, which means the first element of an array is at index 0, the second at index 1, and so on. If you have an array with n elements, the valid indices range from 0 to n - 1. To access an element, you use the array name followed by the index in square brackets. For example, myArray[2] accesses the third element of myArray.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; // Accessing elements int first = numbers[0]; int third = numbers[2]; // Modifying elements numbers[1] = 100; numbers[4] = 500; System.Console.WriteLine("First element: " + first); System.Console.WriteLine("Third element: " + third); System.Console.WriteLine("Updated array:"); for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } } } }

In the code above, you see how to read and update elements in an integer array. To get the first element, you use numbers[0], and for the third, numbers[2]. To change a value, assign a new value to the desired index, such as numbers[1] = 100; to update the second element. After modifying elements, you can loop through the array to see the updated values. Remember, array indices must stay within the valid range; otherwise, you will run into errors.

Program.cs

Program.cs

copy
123456789101112131415
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] data = { 1, 2, 3 }; // Attempt to access an invalid index int invalid = data[5]; System.Console.WriteLine("This will not print: " + invalid); } } }
Note
Note

Arrays in C# are zero-indexed, meaning the first element is at index 0.

1. What is the index of the last element in an array of length 10?

2. How do you change the value of the third element in an array named 'scores'?

3. What exception is thrown if you access an invalid index in a C# array?

question mark

What is the index of the last element in an array of length 10?

Select the correct answer

question mark

How do you change the value of the third element in an array named 'scores'?

Select the correct answer

question mark

What exception is thrown if you access an invalid index in a C# array?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain what happens if I try to access an index outside the array's range?

How do I loop through all elements in a C# array?

Can you show an example of updating multiple elements in an array?

bookAccessing and Modifying Array Elements

Sveip for å vise menyen

Arrays in C# are powerful tools for storing and organizing multiple values of the same type. To make the most of arrays, you need to understand how to access and change their elements. C# arrays use zero-based indexing, which means the first element of an array is at index 0, the second at index 1, and so on. If you have an array with n elements, the valid indices range from 0 to n - 1. To access an element, you use the array name followed by the index in square brackets. For example, myArray[2] accesses the third element of myArray.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; // Accessing elements int first = numbers[0]; int third = numbers[2]; // Modifying elements numbers[1] = 100; numbers[4] = 500; System.Console.WriteLine("First element: " + first); System.Console.WriteLine("Third element: " + third); System.Console.WriteLine("Updated array:"); for (int i = 0; i < numbers.Length; i++) { System.Console.WriteLine(numbers[i]); } } } }

In the code above, you see how to read and update elements in an integer array. To get the first element, you use numbers[0], and for the third, numbers[2]. To change a value, assign a new value to the desired index, such as numbers[1] = 100; to update the second element. After modifying elements, you can loop through the array to see the updated values. Remember, array indices must stay within the valid range; otherwise, you will run into errors.

Program.cs

Program.cs

copy
123456789101112131415
namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] data = { 1, 2, 3 }; // Attempt to access an invalid index int invalid = data[5]; System.Console.WriteLine("This will not print: " + invalid); } } }
Note
Note

Arrays in C# are zero-indexed, meaning the first element is at index 0.

1. What is the index of the last element in an array of length 10?

2. How do you change the value of the third element in an array named 'scores'?

3. What exception is thrown if you access an invalid index in a C# array?

question mark

What is the index of the last element in an array of length 10?

Select the correct answer

question mark

How do you change the value of the third element in an array named 'scores'?

Select the correct answer

question mark

What exception is thrown if you access an invalid index in a C# array?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt