Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Introduction to Arrays | Working with Lists and Arrays
C# Lists & Collections

bookIntroduction to Arrays

Arrays are a fundamental way to store collections of data in C#. An array lets you group several values of the same type together using a single variable name. You can think of an array as a row of boxes, each capable of holding one value, where every box has a fixed position called an index. To use arrays effectively, you need to know how to declare them, initialize them with values, and understand that their size is fixed once set.

To declare an array in C#, specify the type of elements it will hold, followed by square brackets, and then the variable name. For example, to declare an array of integers, you write int[] numbers;. However, this only sets up the variable; to actually use the array, you must initialize it. Initialization means creating the array in memory and setting its size. You do this with the new keyword, such as numbers = new int[5];, which creates an array that can hold five integers.

You can also declare and initialize an array at the same time. For instance, int[] scores = new int[] { 90, 85, 78, 92, 88 }; both declares and fills the array with five values. Remember, arrays in C# have a fixed size—once you set the number of elements, you cannot change it later. If you need a collection that can grow or shrink, you will need other structures, which you will learn about later.

Program.cs

Program.cs

copy
12345678910111213141516171819
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = new int[] { 10, 20, 30, 40, 50 }; Console.WriteLine("Array elements:"); for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } } }

In this code, you first declare and initialize an array of integers named numbers with five values. The array is created using the syntax int[] numbers = new int[] { 10, 20, 30, 40, 50 };. This tells C# to make space for five integers and fill them with the values you provide. Arrays in C# use zero-based indexing, which means the first element is at index 0, the second at index 1, and so on up to the last element at index 4.

To print each number, the code uses a for loop that starts at 0 and continues while i is less than numbers.Length. The Length property gives the total number of elements in the array. Inside the loop, numbers[i] accesses the element at position i. This is how you access or work with individual elements in an array.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] values = new int[] { 5, 10, 15 }; Console.WriteLine("Original values:"); Console.WriteLine(values[0]); Console.WriteLine(values[1]); Console.WriteLine(values[2]); // Update the second element (index 1) values[1] = 20; Console.WriteLine("Updated values:"); Console.WriteLine(values[0]); Console.WriteLine(values[1]); Console.WriteLine(values[2]); } } }

1. What is the main limitation of arrays in C#?

2. Which syntax correctly initializes an array of 5 strings?

3. Fill in the blanks to declare and initialize an array of doubles with 3 elements.

question mark

What is the main limitation of arrays in C#?

Select the correct answer

question mark

Which syntax correctly initializes an array of 5 strings?

Select the correct answer

question-icon

Fill in the blanks to declare and initialize an array of doubles with 3 elements.

double[] { 1.2, 3.4, 5.6;

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookIntroduction to Arrays

Scorri per mostrare il menu

Arrays are a fundamental way to store collections of data in C#. An array lets you group several values of the same type together using a single variable name. You can think of an array as a row of boxes, each capable of holding one value, where every box has a fixed position called an index. To use arrays effectively, you need to know how to declare them, initialize them with values, and understand that their size is fixed once set.

To declare an array in C#, specify the type of elements it will hold, followed by square brackets, and then the variable name. For example, to declare an array of integers, you write int[] numbers;. However, this only sets up the variable; to actually use the array, you must initialize it. Initialization means creating the array in memory and setting its size. You do this with the new keyword, such as numbers = new int[5];, which creates an array that can hold five integers.

You can also declare and initialize an array at the same time. For instance, int[] scores = new int[] { 90, 85, 78, 92, 88 }; both declares and fills the array with five values. Remember, arrays in C# have a fixed size—once you set the number of elements, you cannot change it later. If you need a collection that can grow or shrink, you will need other structures, which you will learn about later.

Program.cs

Program.cs

copy
12345678910111213141516171819
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = new int[] { 10, 20, 30, 40, 50 }; Console.WriteLine("Array elements:"); for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } } }

In this code, you first declare and initialize an array of integers named numbers with five values. The array is created using the syntax int[] numbers = new int[] { 10, 20, 30, 40, 50 };. This tells C# to make space for five integers and fill them with the values you provide. Arrays in C# use zero-based indexing, which means the first element is at index 0, the second at index 1, and so on up to the last element at index 4.

To print each number, the code uses a for loop that starts at 0 and continues while i is less than numbers.Length. The Length property gives the total number of elements in the array. Inside the loop, numbers[i] accesses the element at position i. This is how you access or work with individual elements in an array.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] values = new int[] { 5, 10, 15 }; Console.WriteLine("Original values:"); Console.WriteLine(values[0]); Console.WriteLine(values[1]); Console.WriteLine(values[2]); // Update the second element (index 1) values[1] = 20; Console.WriteLine("Updated values:"); Console.WriteLine(values[0]); Console.WriteLine(values[1]); Console.WriteLine(values[2]); } } }

1. What is the main limitation of arrays in C#?

2. Which syntax correctly initializes an array of 5 strings?

3. Fill in the blanks to declare and initialize an array of doubles with 3 elements.

question mark

What is the main limitation of arrays in C#?

Select the correct answer

question mark

Which syntax correctly initializes an array of 5 strings?

Select the correct answer

question-icon

Fill in the blanks to declare and initialize an array of doubles with 3 elements.

double[] { 1.2, 3.4, 5.6;

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 1
some-alt