Declaring and Initializing Arrays
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.
An array is a fixed-size, ordered collection of elements of the same type.
Program.cs
12345678910111213141516171819202122232425262728293031323334using 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
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?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 3.57
Declaring and Initializing Arrays
Scorri per mostrare il menu
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.
An array is a fixed-size, ordered collection of elements of the same type.
Program.cs
12345678910111213141516171819202122232425262728293031323334using 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
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?
Grazie per i tuoi commenti!