Accessing 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
123456789101112131415161718192021222324252627namespace 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
123456789101112131415namespace 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); } } }
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?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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?
Awesome!
Completion rate improved to 3.57
Accessing and Modifying Array Elements
Veeg om het menu te tonen
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
123456789101112131415161718192021222324252627namespace 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
123456789101112131415namespace 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); } } }
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?
Bedankt voor je feedback!