1. Getting to Know Console.WriteLine
You just wrote your very first C# program and printed your first message to the screen. Congrats! Let's keep going...
Any C# program is made up of commands. There are all kinds of commands. In C#, there are commands for every situation. Each command describes a specific action. At the end of every command, you put a semicolon.
Examples of commands:
| Command | Description (what it does) |
|---|---|
| Console.WriteLine(1); | Prints the number: 1 |
| Console.WriteLine("Amigo"); | Prints the text: Amigo |
| Console.WriteLine("Risha & Amigo"); | Prints the text: Risha & Amigo |
Actually, this is just one command — Console.WriteLine. And in the parentheses you pass parameters to it. Depending on the parameter values, one command can do different things. Super handy.
Letter Case Matters
In C#, case matters: it's important if commands are written in uppercase or lowercase. The command Console.WriteLine() will work, but console.writeline() — won't. If you write a command with the wrong case, C# won't recognize it and won't run it. So, pay attention.
Double Quotes
If you want to print text to the screen, you need to wrap it in double quotes on both sides.
A single quote looks like this ', and a double quote looks like this ". A double quote is not just two singles: don't mix them up.
Double is the one next to the Enter key. Usually, it also has the Cyrillic letter "E" on it.
2. Getting to Know Console.Write
There's another command, similar to Console.WriteLine(), and it's called Console.Write().
The Console.WriteLine() command prints text to the screen and then adds a line break, like hitting Enter. So the next Console.WriteLine() command prints on the next line. And the next one — on the next line, and so on.
Unlike Console.WriteLine(), the Console.Write() command doesn't add Enter after printing text. So, text printed by several Console.Write() commands will all be on the same line.
Mixing Write and WriteLine OutputConsole.Write("Amigo");
Console.WriteLine("The");
Console.Write("Best");
Will print to the screen:
AmigoThe
Best
Console.Write("Amigo");
Console.Write("The");
Console.Write("Best");
Will print to the screen:
AmigoTheBest
If you want to print text on one line, use the Write command, if you want it on several lines — WriteLine. Easy as that.
3. Breaking Down Console.WriteLine
If you know even a little English, learning programming will be way easier. The names of a lot of C# commands are directly related to what they do.
For example, WriteLine is write + line — write a line. I think everyone knows the word write, but why "line"? I'll tell you now. Every line of text on the screen is called a line. So to write a line is to print a line of text.
Console is one of the most common ways for a program and a human to interact. When a user types commands to a program on the keyboard, and it prints text back to the screen, programmers say that the program is working with the console.
In other words, working with the console means printing text to the screen and entering data using the keyboard. So if you ever hear that a program "printed something to the console," it just means it printed text to the screen.
So, Console.WriteLine literally means "Hey, console, print me a line of text."
4. Examples of Printing Different Data
Let's play around with console output. We'll start simple and make it a bit more interesting:
Printing Numbers
Console.WriteLine(1001);
Console.WriteLine(-15);
Printing Text
Console.WriteLine("Programming is awesome!");
Console.WriteLine("We joked, now let's code.");
Printing Multiple Values at Once
Trick: you can add strings together using +. Though it's better to call it concatenation:
Console.WriteLine("My age: " + 28);
Will print to the screen:
My age: 28
You can also concatenate several strings in a row:
Console.WriteLine("Right now it's " + 2025 + " year.");
Will print to the screen:
Right now it's 2025 year.
5. Common Mistakes When Using Console.WriteLine
Young jedis often step on the same rakes. But so you don't become the hero of the book "1001 Mistakes in C#", here's a list of common goofs and tips on how to avoid them.
Mistake 1. Forgot to close a string — missing the second quote:
Console.WriteLine("Oops, opened a quote and forgot to close it);
The compiler will complain "Unterminated string literal" — close your strings!
Mistake 2. Missing semicolon
Console.WriteLine("No ; at the end")
But C# is ruthless. Fix it — add a semicolon at the end of the line.
Mistake 3. Using the wrong quotes
Console.WriteLine(‘Mistake’); // wrong!
Console.WriteLine('Mistake'); // also wrong!
For strings, always use regular double quotes — like these: ".
GO TO FULL VERSION