Print an array in C#
This post will discuss how to print single-dimensional arrays in C#.
1. Using foreach loop
The foreach statement provides a simple, clean way to iterate through the elements of an array. The following example shows the usage of the foreach statement for printing single-dimensional arrays in C#.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; foreach (var item in array) { Console.WriteLine(item); } } } /* Output: 1 2 3 4 5 */ |
The above code uses Console.WriteLine() which outputs each element on a separate line. To print each element on a single line, use Console.Write(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; foreach (int i in array) { Console.Write("{0} ", i); } } } /* Output: 1 2 3 4 5 */ |
We can also use a for-loop to process elements in increasing index order, starting from index 0 till index Length - 1, as shown below: The solution handles trailing delimiting characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; for (int i = 0; i < array.Length; i++) { Console.Write(array[i]); if (i < array.Length - 1) { Console.Write(","); } } } } /* Output: 1,2,3,4,5 */ |
2. Convert to List
Another approach is to use the ToList() method to convert the array into a generic List<T> and then call its ForEach() method to perform the print operation on each element of the List.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; List<int> nums = array.ToList(); nums.ForEach(Console.WriteLine); } } /* Output: 1 2 3 4 5 */ |
This approach is not recommended as it includes the creation of the List as an intermediate step.
3. Using Array.ForEach() method
The recommended solution is to directly call ForEach() method of the Array class rather than using List<T>.ForEach(Action<T>) method. The Array.ForEach<T> method of the Array class performs the specified action on each element of the specified array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; Array.ForEach(array, Console.WriteLine); } } /* Output: 1 2 3 4 5 */ |
4. Using String.Join() method
Another good alternative is to use the String.Join() method, which concatenates the elements of the specified array using the specified separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; Console.WriteLine(String.Join(",", array)); } } /* Output: 1,2,3,4,5 */ |
That’s all about printing an array in C#.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)