Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print multiple blank lines in C#?
In C#, there are several ways to print multiple blank lines to the console. You can use loops, string repetition, or direct method calls depending on your needs.
Using a While Loop
The most straightforward approach is to use a while loop to print blank lines repeatedly −
using System;
namespace Program {
public class Demo {
public static void Main(string[] args) {
int a = 0;
while (a
The output of the above code is −
Displayed 10 blank lines above!
Using a For Loop
A for loop provides a more compact syntax for the same functionality −
using System;
public class Program {
public static void Main() {
for (int i = 0; i
The output of the above code is −
Displayed 5 blank lines above!
Using String Repetition with Environment.NewLine
You can create multiple newlines using string repetition and print them at once −
using System;
public class Program {
public static void Main() {
string blankLines = new string('
', 7);
Console.Write(blankLines);
Console.WriteLine("Displayed 7 blank lines above!");
}
}
The output of the above code is −
Displayed 7 blank lines above!
Using Multiple Console.WriteLine() Calls
For a small number of blank lines, you can call Console.WriteLine() directly multiple times −
using System;
public class Program {
public static void Main() {
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Displayed 3 blank lines above!");
}
}
The output of the above code is −
Displayed 3 blank lines above!
Comparison of Methods
| Method | Best For | Advantage |
|---|---|---|
| While/For Loop | Dynamic number of lines | Flexible, variable-based |
| String Repetition | Large number of lines | Single operation, efficient |
| Multiple WriteLine() | Fixed small number | Simple and readable |
Conclusion
Printing multiple blank lines in C# can be achieved using loops, string repetition, or direct method calls. Choose the method based on whether you need a fixed or dynamic number of blank lines and consider readability for your specific use case.
