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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements