C# Output
Console.WriteLine() is used to print output to the screen, followed by a new line. Console.Write() does the same but without adding a new line at the end.
Both methods can print strings, numbers, and other values, and you can combine text and variables using string concatenation or string interpolation.
Console.WriteLine(value);
Console.Write(value);WriteLine vs Write
Console.WriteLine() adds a newline after the text, so the next output appears on a new line. Console.Write() keeps the cursor on the same line, useful for building output piece by piece.
Printing multiple values
You can combine text and variables with the + operator, or use string interpolation with a $ prefix for cleaner code, covered in a later topic.
using System;
class Program {
static void Main() {
Console.WriteLine("Hello");
Console.WriteLine("World");
}
}Hello
WorldEach WriteLine call prints text followed by a new line.
using System;
class Program {
static void Main() {
Console.Write("Hello ");
Console.Write("World");
}
}Hello WorldWrite() does not add a new line, so both texts appear on the same line.
Key points
- Console.WriteLine() prints text and moves to a new line.
- Console.Write() prints text without a new line.
- You can print strings, numbers, and other data types.
- The + operator can join strings and variables together.
