The console, the project and week review
This lesson closes week 1. We will look at the Console class in more detail, see what is inside a .NET project and practice with a complete program. At the end there is a quiz to check what you learned.
Writing to the console
Console.WriteLine writes and moves to a new line; Console.Write writes without moving, which is useful to ask for data on the same line. You can mix text and values with the + operator or, better, with interpolation, which you will study in depth next week. To insert a line break inside text use the \n sequence, and to write a double quote inside text use \".
Console.Write("Loading");
Console.Write("...");
Console.WriteLine(" done");
Console.WriteLine("Line one\nLine two");
Console.WriteLine("She said \"hello\"");
Console.WriteLine(@"C:\Users\Ana\file.txt"); // verbatim string: no escapesReading what the user types
Console.ReadLine() waits for the person to type and press Enter; it returns what was typed as a string. It may return null if the input is closed (for example when redirecting an empty file), and that is why its type is string?. We will study this detail in week 5; for now, if you need a safe value, you can write ?? "" after the call, which means "if it is null, use empty text".
Console.Write("What is your name? ");
string name = Console.ReadLine() ?? "";
Console.WriteLine("Nice to meet you, " + name + "!");What is inside the project
- Program.cs: your code. You can add more .cs files and the compiler joins them.
- ProjectName.csproj: an XML file with the target framework (net9.0), and whether implicit usings and nullability are enabled.
- bin/ and obj/: build output. You can delete them safely and they are regenerated.
When you share your project, do not include bin or obj; the .csproj and the .cs files are enough. Another person runs dotnet run and everything is rebuilt. A console program ends when the last statement finishes; if you run it by double clicking the .exe the window closes instantly, which is why it is best to launch it from the terminal.
A complete mini program
Let us put it all together: a program that asks for a name and a birth year and works out an approximate age. We cannot safely convert text to numbers yet (that is the topic of week 2), so here we practice variables and formatted output with fixed values.
Console.Write("Your name: ");
string name = Console.ReadLine() ?? "";
int birthYear = 1995;
int currentYear = DateTime.Now.Year;
int approxAge = currentYear - birthYear;
Console.WriteLine("Hello, " + name + "!");
Console.WriteLine("Born in " + birthYear + ", about " + approxAge + " years old.");A good habit from day one is keeping the code tidy: one statement per line, four-space indentation inside blocks and blank lines to separate ideas. VS Code can format the file for you with the Shift+Alt+F shortcut. Also, write comments only when they explain the reason for something; a comment that repeats what the line does adds nothing and becomes outdated when you change the code.
Teste dein Wissen
Frage 1 von 7Which command creates a console project?
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen