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

ProgrammingServer Side ProgrammingCsharp

Set an array.

int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };

Now, if you will print this array using a loop and new line, then the arrays will be visible vertically.

To work it horizontally, use the Join() method and set spaces to separate array elements.

string.Join(" ", array)

Let us see the complete code.

Example

 Live Demo

using System;
using System.Linq;
using System.IO;
class Program {
   static void Main() {
      int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 };
      Console.WriteLine(string.Join(" ", array));
   }
}

Output

50 100 150 200 250 300 350 400
raja
Published on 11-Sep-2018 11:59:03
Advertisements