Arrays in Dotnet framework with example
Arrays in Dotnet framework
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Arrays in Dotnet framework with example
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted May 20, 2025, 1:12 AM EDT
In the .NET Framework, arrays are a way to store multiple values of the same data type in a single variable. Arrays are fixed in size once declared.
Single-Dimensional Array:
using System; class Program { static void Main() { // Declare and initialize an array of integers int[] numbers = new int[5]; // Array of size 5 // Assign values numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; // Access and print array elements for (int i = 0; i < numbers.Length; i++) { Console.WriteLine("Element at index " + i + ": " + numbers[i]); } } }Multi-Dimensional Array:
int[,] matrix = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); }Jagged Array (Array of Arrays):
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[] { 1, 2 }; jaggedArray[1] = new int[] { 3, 4, 5 }; jaggedArray[2] = new int[] { 6 }; for (int i = 0; i < jaggedArray.Length; i++) { for (int j = 0; j < jaggedArray[i].Length; j++) { Console.Write(jaggedArray[i][j] + " "); } Console.WriteLine(); }Initializing an Array Directly:
string[] fruits = { "Apple", "Banana", "Cherry" }; foreach (string fruit in fruits) { Console.WriteLine(fruit); }