Arrays and Collections
In .NET, arrays and collections are used to store multiple values in a single variable. They provide a convenient way to work with groups of related data.
Arrays
An array is a fixed-size collection of elements of the same type. It allows you to store and access multiple values using a single variable. The elements in an array are accessed by their index, which starts at 0.
Here's an example of initializing and accessing elements in an array:
TEXT/X-CSHARP
1string[] names = { "Alice", "Bob", "Charlie", "Dave" };
2
3Console.WriteLine(names[0]); // Output: Alice
4Console.WriteLine(names[2]); // Output: Charliexxxxxxxxxx23
using System;class Program{ static void Main() { // Arrays string[] names = { "Alice", "Bob", "Charlie", "Dave" }; Console.WriteLine("Names:"); foreach (string name in names) { Console.WriteLine(name); } // Collections List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 }; Console.WriteLine("Numbers:"); foreach (int number in numbers) { Console.WriteLine(number); } }}OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment



