Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Declaring and Initializing Arrays | Array Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
C# Arrays

bookDeclaring and Initializing Arrays

Prerequisites
Prerequisites

Arrays are a fundamental concept in C# that allow you to store multiple values of the same type under a single variable name. They are useful when you need to manage collections of data, such as a list of numbers or words, without creating separate variables for each item. In C#, arrays store data in a fixed-size, ordered sequence, which means every element can be accessed by its numerical index, starting from zero.

Note
Definition

An array is a fixed-size, ordered collection of elements of the same type.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Declare an integer array with 3 elements int[] numbers = new int[3]; // Assign values to each element numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; // Declare and initialize a string array with 2 elements string[] names = new string[2]; names[0] = "Alice"; names[1] = "Bob"; // Print the arrays Console.WriteLine("Integer array:"); Console.WriteLine(numbers[0]); Console.WriteLine(numbers[1]); Console.WriteLine(numbers[2]); Console.WriteLine("String array:"); Console.WriteLine(names[0]); Console.WriteLine(names[1]); } } }

In the code above, you first declare an integer array called numbers with a size of 3. This means the array can hold exactly three integers. When you create an array with new int[3], each element is automatically set to the default value for that typeβ€”in this case, 0 for integers. You then assign values to each index: numbers[0] = 10, numbers[1] = 20, and numbers[2] = 30. Similarly, you declare a string array named names with two elements and assign values to both. Arrays in C# are always zero-indexed, so the first element is at index 0. If you try to access an index outside the range (for example, numbers[3]), you will get a runtime error.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Initialize an integer array with explicit values int[] scores = { 85, 92, 78, 90 }; // Initialize a string array with explicit values string[] fruits = { "Apple", "Banana", "Cherry" }; // Print all elements and the array length Console.WriteLine("Scores array:"); for (int i = 0; i < scores.Length; i++) { Console.WriteLine(scores[i]); } Console.WriteLine("Scores array length: " + scores.Length); Console.WriteLine("Fruits array:"); for (int i = 0; i < fruits.Length; i++) { Console.WriteLine(fruits[i]); } Console.WriteLine("Fruits array length: " + fruits.Length); } } }

1. What is the primary characteristic of an array in C#?

2. Which of the following is a correct way to declare an array of 5 integers?

question mark

What is the primary characteristic of an array in C#?

Select the correct answer

question mark

Which of the following is a correct way to declare an array of 5 integers?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookDeclaring and Initializing Arrays

Swipe to show menu

Prerequisites
Prerequisites

Arrays are a fundamental concept in C# that allow you to store multiple values of the same type under a single variable name. They are useful when you need to manage collections of data, such as a list of numbers or words, without creating separate variables for each item. In C#, arrays store data in a fixed-size, ordered sequence, which means every element can be accessed by its numerical index, starting from zero.

Note
Definition

An array is a fixed-size, ordered collection of elements of the same type.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031323334
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Declare an integer array with 3 elements int[] numbers = new int[3]; // Assign values to each element numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; // Declare and initialize a string array with 2 elements string[] names = new string[2]; names[0] = "Alice"; names[1] = "Bob"; // Print the arrays Console.WriteLine("Integer array:"); Console.WriteLine(numbers[0]); Console.WriteLine(numbers[1]); Console.WriteLine(numbers[2]); Console.WriteLine("String array:"); Console.WriteLine(names[0]); Console.WriteLine(names[1]); } } }

In the code above, you first declare an integer array called numbers with a size of 3. This means the array can hold exactly three integers. When you create an array with new int[3], each element is automatically set to the default value for that typeβ€”in this case, 0 for integers. You then assign values to each index: numbers[0] = 10, numbers[1] = 20, and numbers[2] = 30. Similarly, you declare a string array named names with two elements and assign values to both. Arrays in C# are always zero-indexed, so the first element is at index 0. If you try to access an index outside the range (for example, numbers[3]), you will get a runtime error.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
// File: Program.cs using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Initialize an integer array with explicit values int[] scores = { 85, 92, 78, 90 }; // Initialize a string array with explicit values string[] fruits = { "Apple", "Banana", "Cherry" }; // Print all elements and the array length Console.WriteLine("Scores array:"); for (int i = 0; i < scores.Length; i++) { Console.WriteLine(scores[i]); } Console.WriteLine("Scores array length: " + scores.Length); Console.WriteLine("Fruits array:"); for (int i = 0; i < fruits.Length; i++) { Console.WriteLine(fruits[i]); } Console.WriteLine("Fruits array length: " + fruits.Length); } } }

1. What is the primary characteristic of an array in C#?

2. Which of the following is a correct way to declare an array of 5 integers?

question mark

What is the primary characteristic of an array in C#?

Select the correct answer

question mark

Which of the following is a correct way to declare an array of 5 integers?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1
some-alt