How to print the contents of array horizontally using C#?

Printing array contents horizontally in C# means displaying all elements in a single line, separated by spaces or other delimiters. This is useful for creating formatted output where elements appear side by side rather than vertically.

When you print an array using a regular loop with Console.WriteLine(), each element appears on a new line. To display elements horizontally, C# provides several approaches.

Syntax

Using string.Join() method −

string.Join(separator, array)

Using Console.Write() in a loop −

for (int i = 0; i < array.Length; i++) {
    Console.Write(array[i] + " ");
}

Using string.Join() Method

The string.Join() method is the most efficient way to print array elements horizontally. It concatenates all elements with a specified separator −

using System;

class Program {
    static void Main() {
        int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };
        Console.WriteLine("Array elements horizontally:");
        Console.WriteLine(string.Join(" ", array));
    }
}

The output of the above code is −

Array elements horizontally:
50 100 150 200 250 300 350 400

Using Console.Write() Method

You can also use Console.Write() in a loop to print elements horizontally with custom formatting −

using System;

class Program {
    static void Main() {
        int[] array = new int[] { 10, 20, 30, 40, 50 };
        Console.Write("Array elements: ");
        
        for (int i = 0; i < array.Length; i++) {
            Console.Write(array[i]);
            if (i < array.Length - 1) {
                Console.Write(", ");
            }
        }
        Console.WriteLine(); // Move to next line
    }
}

The output of the above code is −

Array elements: 10, 20, 30, 40, 50

Using Different Separators

The string.Join() method allows you to use different separators for formatting −

using System;

class Program {
    static void Main() {
        string[] fruits = { "Apple", "Banana", "Cherry", "Date" };
        
        Console.WriteLine("With spaces: " + string.Join(" ", fruits));
        Console.WriteLine("With commas: " + string.Join(", ", fruits));
        Console.WriteLine("With dashes: " + string.Join(" - ", fruits));
        Console.WriteLine("With pipes: " + string.Join(" | ", fruits));
    }
}

The output of the above code is −

With spaces: Apple Banana Cherry Date
With commas: Apple, Banana, Cherry, Date
With dashes: Apple - Banana - Cherry - Date
With pipes: Apple | Banana | Cherry | Date

Comparison of Methods

Method Advantages Best Used For
string.Join() Clean, efficient, built-in Simple horizontal display with consistent separators
Console.Write() loop More control over formatting Custom formatting or conditional display logic

Conclusion

To print array contents horizontally in C#, use string.Join() for simple cases with consistent separators, or Console.Write() in a loop for more complex formatting needs. The string.Join() method is generally the most efficient and readable approach.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements