Console.WriteLine() and Console.Write() in C#?

A C# console program is a program that runs in a console or command-line interface, where the user interacts with the program by typing commands and receiving text output. C# Console programs are useful for a wide range of applications, such as system utilities, file management, and data processing.

C# console program

Following is an example of a simple C# console program that demonstrates the use of Console.Write() and Console.WriteLine():

using System; class Program { static void Main() { Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine("Hello, " + name + "!"); } }
//Output: Enter your name: doe Hello, doe!

This program prompts the user to enter their name, reads the input from the console using Console.ReadLine(), and then outputs a greeting message that includes the entered name using Console.WriteLine().

Let's take a closer look at the Console.Write() and Console.WriteLine() methods:

Console.Write()

This method writes a specified string to the console without a newline character at the end. In the example above, Console.Write("Enter your name: "); displays the prompt "Enter your name: " to the console without adding a newline character.

Console.WriteLine()

This method writes a specified string to the console followed by a newline character. In the example above, Console.WriteLine("Hello, " + name + "!"); writes the greeting message "Hello, [name]!" to the console, where [name] is the value entered by the user, and adds a newline character at the end.

Difference | Console.Write() and Console.WriteLine()


How to C# console applications

The difference between Console.Write() and Console.WriteLine() is that Console.Write() keeps the cursor on the same line after the text is written, while Console.WriteLine() moves the cursor to the next line. In our example, this means that the prompt and the user input are displayed on the same line, while the greeting message is displayed on a new line.

Overall, Console.Write() and Console.WriteLine() are useful methods for writing text to the console in C# console programs. They provide a flexible and customizable way to interact with users and display output.