Console.WriteLine() / Console.Write()
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
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("Tanaka");
Console.WriteLine(" san"); // Newline is added here.
// Embed values using a format string.
string name = "Yamada";
int age = 30;
Console.WriteLine("{0} is {1} years old.", name, age);
// String interpolation ($"...") offers a more intuitive syntax.
Console.WriteLine($"{name} is {age} years old.");
// Format numbers with format specifiers.
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}");
// Output text in a different color.
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error message (red)");
Console.ResetColor();
Console.WriteLine("Back to the default color.");
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.