Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Accessing and Modifying Array Elements | Array Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3
some-alt