Console.WriteLine() / Console.Write()
| Since: | C# 1.0(2002) |
|---|
Console.WriteLine() and Console.Write() output text to the console. They are frequently used in debugging and learning programs.
Syntax
// Outputs text and moves to the next line. Console.WriteLine(value) Console.WriteLine(string format, params object[] args) // Outputs text without moving to the next line. Console.Write(value) Console.Write(string format, params object[] args)
Method List
| Method | Description |
|---|---|
| Console.WriteLine(value) | Converts a value to a string, outputs it, and appends a newline. Calling it with no arguments outputs a blank line. |
| Console.Write(value) | Outputs a value without a newline. Use this when you want to output multiple values on the same line. |
| Console.WriteLine(format, arg0, arg1) | Outputs values embedded in a format string using placeholders ({0}, {1}). |
| Console.ForegroundColor | Sets the text color of the output (specify a ConsoleColor enum value). |
| Console.ResetColor() | Resets the text color and background color to their defaults. |
Sample Code
Program.cs
using System;
// Basic output
Console.WriteLine("Hello, World!");
Console.WriteLine(42);
Console.WriteLine(3.14);
Console.WriteLine(true);
// Write() does not add a newline
Console.Write("Name: ");
Console.Write("Gojo Satoru");
Console.WriteLine(" san"); // Newline is added here
// Embed values using a format string
string name = "Itadori Yuji";
int age = 15;
Console.WriteLine("{0} is {1} years old.", name, age);
// String interpolation ($"...") offers a more intuitive syntax
Console.WriteLine($"{name} is {age} years old.");
Run the following command:
dotnet run Hello, World! 42 3.14 True Name: Gojo Satoru san Itadori Yuji is 15 years old. Itadori Yuji is 15 years old.
Format Specifiers
You can format numbers, dates, percentages, and more.
FormatSample.cs
using System;
double price = 1234.5;
Console.WriteLine($"Price: {price:N0} yen"); // Integer with thousands separator
Console.WriteLine($"Rate: {0.756:P1}"); // Percentage with 1 decimal place
Console.WriteLine($"Date: {DateTime.Now:yyyy/MM/dd}");
Console.WriteLine($"Fixed decimal: {price:F2}"); // 2 decimal places
Console.WriteLine($"Hex: {255:X}"); // Hexadecimal
Run the following command:
dotnet run Price: 1,234 yen Rate: 75.6% Date: 2026/03/31 Fixed decimal: 1234.50 Hex: FF
Changing Text Color
Use Console.ForegroundColor to change the text color. After use, call Console.ResetColor() to restore the default color.
ColorOutput.cs
using System;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error message (red)");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Success message (green)");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Warning message (yellow)");
Console.ResetColor();
Console.WriteLine("Back to the default color.");
Run the following command:
dotnet run Error message (red) Success message (green) Warning message (yellow) Back to the default color.
Common Mistakes
Common Mistake: Forgetting a Newline After Write()
If you do not add a newline after Console.Write(), the next output will appear on the same line.
using System;
// NG: Without a newline after Write(), the next output is appended on the same line
Console.Write("Processing...");
Console.WriteLine("Done"); // Outputs "Processing...Done" on one line
The following example demonstrates this:
dotnet run Processing...Done
The following example demonstrates this:
using System;
// OK: Add a newline after Write() when needed
Console.Write("Processing...");
Console.WriteLine(); // Output newline only
Console.WriteLine("Done");
The following example demonstrates this:
dotnet run Processing... Done
Notes
Console.WriteLine() is the most commonly used output method in C#. String interpolation ($"...") lets you embed variables directly into a string, resulting in more readable code than format strings ({0}).
Useful numeric format specifiers include :N0 (integer with thousands separator), :F2 (2 decimal places), and :P1 (percentage with 1 decimal place). For a full list of format specifiers, refer to the .NET format string documentation.
To read input from the console, see Console.ReadLine() / Console.ReadKey().
If you find any errors or copyright issues, please contact us.