C# Output
C# output is the text and numbers a program prints to the screen. Console.WriteLine prints a value and then ends the line.
WriteLine ends the line
Console is the text window of a program. Each WriteLine call ends its own line.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
Console.WriteLine("World");
}
}Write stays together
Neither Write call ends the line. The two parts share one output line.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Hello");
Console.Write(" World");
}
}Numbers as values
A whole number prints without quotes. An expression prints its result.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(42);
Console.WriteLine(3 + 4);
}
}Example
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Ticket ");
Console.WriteLine(7);
Console.Write("Row ");
Console.WriteLine(12);
}
}Key points
- WriteLine prints a value and ends the line.
- Write prints a value and stays on the same line.
- Text prints inside double quotes.
- Numbers print without quotes.
In this example, Write prints Ticket and stays on the line. WriteLine prints 7 beside it and ends the line. The next pair prints Row 12 the same way.
Note
WriteLine and Write are both methods on Console.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz C# Compiler
Exercise
What does Write do after it prints a value?