-3

There are multiple Console.WriteLine() arguments between some values, but I don't want to give them all separately by copypaste, is there any way I can specify the number of WriteLine arguments with just one argument and it'll be done .

here is some example

int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };// giving values
        int[] arr2 = { 34444, 111 };
        loop();//calling the method
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine();
        printArray(arr1);//passing arrays
        Console.WriteLine();
        Console.WriteLine();
        printArray(arr2);
        Console.WriteLine();
        Console.WriteLine();
        minArray(arr1);
        Console.WriteLine();
        Console.WriteLine();
        minArray(arr2);
        Console.WriteLine();
        Console.WriteLine();
        maxArray(arr1);
        Console.WriteLine();
        Console.WriteLine();
        maxArray(arr2); 
  • 1
    On the face of it, no, but maybe you could provide some actual code to illustrate what you're talking about. – John Jul 18 '22 at 07:56
  • 1
    Please provide some code to illustrate what you want to do. – phuzi Jul 18 '22 at 07:57
  • i have give some example, now –  Jul 18 '22 at 07:58
  • 1
    Why not using loops? – Cid Jul 18 '22 at 07:58
  • 2
    public static void WriteLines(int count), use a for-loop. – Hans Passant Jul 18 '22 at 07:59
  • You can call one Console.WriteLine with as much `\n` as you need. – aca Jul 18 '22 at 07:59
  • @Cid Since I have many values to pass one after another, I have to give specific write lines between the values, so I don't think creating one forloop with a defined number of lines is a good idea for me. If you can help me figure out how to change the values of the for loop, please let me know. –  Jul 18 '22 at 08:01
  • @HansPassant can u show it in detail please , it looks like it will solve the issue –  Jul 18 '22 at 08:03
  • You can use `Console.Write(new String(Environment.NewLine, NumberOfDesiredNewLines));` (Note the usage of `Write` instead of `WriteLine` – Cid Jul 18 '22 at 08:09

2 Answers2

2

The best approach would be to create a function that prints '\n' (symbol of new line) as many times as you need. new String can help us with repetition of '\n'.
So the function might look like this:

static void PrintNewLine(int n)
{
    string newLines = new String('\n',n-1);
    Console.WriteLine(newLines);
}

You might notice n-1 inside the function.
We added it just because WriteLine puts one \n into console itself.

Here is an example of usage:

static void Main() {
    Console.WriteLine("Hello");
    PrintNewLine(5);
    Console.WriteLine("Another Hello");
}

It will print Hello followed by 5 blank lines followed by Another Hello

Jaood_xD
  • 808
  • 2
  • 4
  • 16
0

You could also try this

Console.WriteLine("Hello");
Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Repeat(string.Empty, 6)));
Console.WriteLine("World");

This will print six empty lines between Hello and World.

Gec
  • 428
  • 2
  • 10