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.
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
string.Join works with any array type because it calls ToString() on each element.
foreach Loop
Array.ForEach
A static method that applies an action to each element:
LINQ Methods
Printing Multi-Dimensional Arrays
Printing Arrays of Objects
Quick Debugging with Debug/Trace
Comparison of Methods
| Method | Best For | One-liner? |
string.Join | Simple delimited output | Yes |
foreach | Custom formatting per element | No |
Array.ForEach | Action per element, functional style | Yes |
LINQ .Select | Transforming before printing | Yes |
for loop | When you need the index | No |
Common Pitfalls
- Calling
Console.WriteLine(array)directly: This prints the type name (System.Int32[]), not the contents. Always usestring.Joinor a loop. - Forgetting
ToString()override on custom objects: Without it,string.Joinprints the class name for every element. OverrideToString()for meaningful output. - Using
+in a loop for string building: Concatenating strings in a loop creates many intermediate allocations. UseStringBuilderorstring.Joinfor large arrays. Array.ForEachvsList.ForEach:Array.ForEachis a static method (Array.ForEach(arr, action)).List<T>.ForEachis an instance method (list.ForEach(action)). Mixing them up causes compile errors.- Printing null elements: If the array contains null references,
string.Joinprints empty strings for nulls. Usen?.ToString() ?? "null"to make nulls visible in output.
Summary
- Use
string.Join(", ", array)for quick, one-line array printing - Use
foreachorforloops 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)andGetLength(1) - Never call
Console.WriteLine(array)directly — it prints the type name, not the contents
Related reading
- Printing all possible subsets of a list
- Printing all possible words from a 2D array of characters
- Printing BFS Binary Tree in Level Order with Specific Formatting
- Printing HashMap In Java
- Priority queue in .Net
- Problems creating a Foreign-Key relationship on Entity Framework
- Printing Lists as Tabular Data
- Priority Queue in swift

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 courseTrack 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.