Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Indexación | Arreglos
Fundamentos de C#

bookIndexación

Every element of an array has an index which is simply its position in the array. The first element has the index 0, the second element has the index 1, and so on. Since indices start from 0, the last element's index is 1 less than the size of the array (size - 1).

The following illustration is a slightly modified version of the one from the last chapter however this time the elements have their corresponding indices written beneath them:

We can access an element of an array using the following syntax:

main.cs

main.cs

copy
1
arrayName[index];

Let's consider the example of accessing the array element by it's index:

main.cs

main.cs

copy
12345678910111213
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] studentMarks = new int[50]; Console.WriteLine(studentMarks[9]); // Output: 0 } } }

In the above code, we access the 10th element of the array and it outputs 0. This is because when we create a new empty array it is automatically filled with relevant zero values according to its data type.

Each element of an array is essentially a variable.

We can access and modify an element of an array using the following syntax:

main.cs

main.cs

copy
1
arrayName[index] = newValue;

For example:

main.cs

main.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] studentMarks = new int[50]; studentMarks[0] = 50; studentMarks[1] = 77; studentMarks[2] = 97; Console.WriteLine(studentMarks[0]); Console.WriteLine(studentMarks[1]); Console.WriteLine(studentMarks[2]); } } }
question mark

What will be the output of the following code?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain more about how to modify array elements in C#?

What happens if I try to access an index that doesn't exist in the array?

Can you show more examples of using the length property with arrays?

bookIndexación

Desliza para mostrar el menú

Every element of an array has an index which is simply its position in the array. The first element has the index 0, the second element has the index 1, and so on. Since indices start from 0, the last element's index is 1 less than the size of the array (size - 1).

The following illustration is a slightly modified version of the one from the last chapter however this time the elements have their corresponding indices written beneath them:

We can access an element of an array using the following syntax:

main.cs

main.cs

copy
1
arrayName[index];

Let's consider the example of accessing the array element by it's index:

main.cs

main.cs

copy
12345678910111213
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] studentMarks = new int[50]; Console.WriteLine(studentMarks[9]); // Output: 0 } } }

In the above code, we access the 10th element of the array and it outputs 0. This is because when we create a new empty array it is automatically filled with relevant zero values according to its data type.

Each element of an array is essentially a variable.

We can access and modify an element of an array using the following syntax:

main.cs

main.cs

copy
1
arrayName[index] = newValue;

For example:

main.cs

main.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] studentMarks = new int[50]; studentMarks[0] = 50; studentMarks[1] = 77; studentMarks[2] = 97; Console.WriteLine(studentMarks[0]); Console.WriteLine(studentMarks[1]); Console.WriteLine(studentMarks[2]); } } }
question mark

What will be the output of the following code?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
some-alt