Print to the screen and read user input with Console.WriteLine and ReadLine, and learn the difference between the classic Main method and modern top-level statements.
The starting point of a program
Every program has a starting point: the place where the computer begins to run it. In C#, for many years this point was a method called Main. In modern C# (since version 9), thanks to top-level statements, you do not have to write this skeleton; the compiler generates it for you.
The new style: top-level statements
The single line the template gives you is a complete program:
Console.WriteLine("Hello, Byte Akademi!");
The classic style: the Main method
In older books and in some projects you will see the following form. Both do the same job:
using System;
namespace HelloWorld
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, Byte Akademi!");
}
}
}
Here using System; means "I will use the ready-made classes in the System namespace"; the Console class lives there. In new .NET projects, commonly used namespaces are added behind the scenes (this is called implicit usings), which is why you could use Console in the new style without writing using.
Printing: Write and WriteLine
Console.WriteLine moves to the next line after printing; Console.Write does not.
Console.Write("Voltage: ");
Console.Write(12);
Console.WriteLine(" V");
Console.WriteLine("This line starts below.");
Voltage: 12 V
This line starts below.
Reading user input: ReadLine
Console.ReadLine() returns the line the user typed as text. If the user closes the input stream without typing anything, it may return null; that is why the return type is written as string? (the question mark means "can be empty"; we will see this in later lessons).
Console.Write("What is your name? ");
string? name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
What is your name? Fatih
Hello, Fatih!
The $ sign before the quotation mark means string interpolation: the variable or expression you write inside the curly braces is placed into the text. It is much more readable than joining pieces of text with +.
Comments
You can write notes inside your code that do not affect how the program runs. Your future self (or someone else) will thank you for them.
// A single-line comment
/* A comment that
spans several lines */
Console.WriteLine("Comments do not run"); // an end-of-line comment is fine too
Warning: C# is case-sensitive. console.writeline gives an error; the correct form is Console.WriteLine. Do not forget to put ; at the end of every statement either.
Escape sequences
A backslash is used for special characters inside text: \n is a new line, \t is a tab and \" is a quotation mark.
Console.WriteLine("Channel\tValue");
Console.WriteLine("CH1\t3.3 V");
Console.WriteLine("A \"quoted\" word");
Reading compiler error messages
When you make a mistake, the compiler prints the file name, the line number and an error code (such as CS1002). Read the whole message to the end: most of the time it tells you directly what the problem is and where it is. If there are several errors, fix the topmost one first; the ones below are often side effects of it. For the time it will save you in programming, this habit is one of the most valuable skills.
Try it yourself
Write a program that asks the user for their name and their department, then prints a single sentence such as "Hello Alice, you study Electrical-Electronics Engineering." Use two separate ReadLine calls and one interpolated string. Then try turning the output into two columns with \t.
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.