C#
arrays
programming
coding
.NET

printing all contents of array in C

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

C# provides several ways to print all elements of an array. The simplest is string.Join(), which converts the entire array to a delimited string in one call. For more control, foreach loops and Array.ForEach() let you format each element individually. LINQ's Select and Aggregate methods handle complex formatting. The right choice depends on whether you need simple output for debugging or formatted output for display.

string.Join — Simplest Approach

csharp
1int[] numbers = { 1, 2, 3, 4, 5 };
2
3// Print all elements separated by comma
4Console.WriteLine(string.Join(", ", numbers));
5// Output: 1, 2, 3, 4, 5
6
7string[] fruits = { "apple", "banana", "cherry" };
8Console.WriteLine(string.Join(" | ", fruits));
9// Output: apple | banana | cherry
10
11// Newline-separated
12Console.WriteLine(string.Join(Environment.NewLine, numbers));
13// 1
14// 2
15// 3
16// 4
17// 5

string.Join works with any array type because it calls ToString() on each element.

foreach Loop

csharp
1int[] numbers = { 10, 20, 30, 40, 50 };
2
3foreach (int num in numbers)
4{
5    Console.WriteLine(num);
6}
7
8// With index tracking
9for (int i = 0; i < numbers.Length; i++)
10{
11    Console.WriteLine($"[{i}] = {numbers[i]}");
12}
13// [0] = 10
14// [1] = 20
15// [2] = 30
16// [3] = 40
17// [4] = 50

Array.ForEach

A static method that applies an action to each element:

csharp
1string[] names = { "Alice", "Bob", "Charlie" };
2
3Array.ForEach(names, name => Console.WriteLine(name));
4// Alice
5// Bob
6// Charlie
7
8// With formatting
9Array.ForEach(names, name => Console.WriteLine($"Name: {name}"));

LINQ Methods

csharp
1using System.Linq;
2
3int[] numbers = { 1, 2, 3, 4, 5 };
4
5// Select with index
6numbers.Select((n, i) => $"[{i}] {n}")
7       .ToList()
8       .ForEach(Console.WriteLine);
9// [0] 1
10// [1] 2
11// [2] 3
12
13// Aggregate for custom string building
14string result = numbers.Aggregate("", (acc, n) => acc + n + " ").Trim();
15Console.WriteLine(result);
16// 1 2 3 4 5

Printing Multi-Dimensional Arrays

csharp
1// 2D array
2int[,] matrix = {
3    { 1, 2, 3 },
4    { 4, 5, 6 },
5    { 7, 8, 9 }
6};
7
8for (int row = 0; row < matrix.GetLength(0); row++)
9{
10    for (int col = 0; col < matrix.GetLength(1); col++)
11    {
12        Console.Write($"{matrix[row, col],4}");
13    }
14    Console.WriteLine();
15}
16//    1   2   3
17//    4   5   6
18//    7   8   9
19
20// Jagged array
21int[][] jagged = { new[] { 1, 2 }, new[] { 3, 4, 5 }, new[] { 6 } };
22foreach (int[] inner in jagged)
23{
24    Console.WriteLine(string.Join(", ", inner));
25}
26// 1, 2
27// 3, 4, 5
28// 6

Printing Arrays of Objects

csharp
1// Custom class — override ToString for meaningful output
2class Student
3{
4    public string Name { get; set; }
5    public int Grade { get; set; }
6
7    public override string ToString() => $"{Name} (Grade: {Grade})";
8}
9
10Student[] students = {
11    new Student { Name = "Alice", Grade = 95 },
12    new Student { Name = "Bob", Grade = 87 },
13    new Student { Name = "Charlie", Grade = 92 }
14};
15
16Console.WriteLine(string.Join("\n", students));
17// Alice (Grade: 95)
18// Bob (Grade: 87)
19// Charlie (Grade: 92)
20
21// Without ToString override, you get the type name:
22// Student
23// Student
24// Student

Quick Debugging with Debug/Trace

csharp
1using System.Diagnostics;
2
3int[] data = { 100, 200, 300 };
4
5// Output to debug window (Visual Studio Output panel)
6Debug.WriteLine($"Data: [{string.Join(", ", data)}]");
7
8// Conditional debug output
9Trace.WriteLineIf(data.Length > 0, $"Array has {data.Length} elements");

Comparison of Methods

MethodBest ForOne-liner?
string.JoinSimple delimited outputYes
foreachCustom formatting per elementNo
Array.ForEachAction per element, functional styleYes
LINQ .SelectTransforming before printingYes
for loopWhen you need the indexNo

Common Pitfalls

  • Calling Console.WriteLine(array) directly: This prints the type name (System.Int32[]), not the contents. Always use string.Join or a loop.
  • Forgetting ToString() override on custom objects: Without it, string.Join prints the class name for every element. Override ToString() for meaningful output.
  • Using + in a loop for string building: Concatenating strings in a loop creates many intermediate allocations. Use StringBuilder or string.Join for large arrays.
  • Array.ForEach vs List.ForEach: Array.ForEach is a static method (Array.ForEach(arr, action)). List<T>.ForEach is an instance method (list.ForEach(action)). Mixing them up causes compile errors.
  • Printing null elements: If the array contains null references, string.Join prints empty strings for nulls. Use n?.ToString() ?? "null" to make nulls visible in output.

Summary

  • Use string.Join(", ", array) for quick, one-line array printing
  • Use foreach or for loops when you need custom formatting or index access
  • Override ToString() on custom classes so array printing shows meaningful data
  • For 2D arrays, use nested loops with GetLength(0) and GetLength(1)
  • Never call Console.WriteLine(array) directly — it prints the type name, not the contents

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.