Search code examples
c#console.writeline

With only one console writeline, how can I give multiple console writelines?


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); 

Solution

  • 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