Module 1 · First Steps · Lesson 1 of 37
Your First C# Program: Build, Run and Understand It
Start here
You need a computer with the .NET 10 SDK installed, a text editor, and a terminal. A terminal is a window where you enter commands. No earlier programming knowledge is assumed. This course uses ordinary project-based console applications and stable C# 14. A console application reads or writes text through its standard input and output.
By the end, you will be able to:
- Create a console project and distinguish its source file from its project settings.
- Predict the lines printed by a small program before running it.
- Separate a compilation problem from a program that runs but produces the wrong answer.
Our continuing example is StudyDeck, a study-task planner. We will build understanding in small, independent programs before combining features into a later capstone. Today's program only prints a plan. It does not save tasks, read a database, or require an account. The analogy section offers another way to picture the edit–build–run cycle.
Create a project you can explain
An SDK is a software development kit: tools for creating, compiling, and running programs. The .NET runtime executes .NET applications; installing only a runtime is not sufficient for the development commands in this lesson.
In a directory where you want a new learning project, enter:
dotnet --version dotnet new console --framework net10.0 --name StudyDeck.First cd StudyDeck.First dotnet run
The version command should identify a .NET 10 SDK selected for that directory. If dotnet is not found, resolve the SDK installation or terminal environment before editing code. Do not diagnose that as a mistake in Console.WriteLine.
The new console command creates starter files from a template. The project file ending in .csproj describes settings such as the target framework. Program.cs contains the initial C# source. A target framework names the .NET API baseline the project targets; here it is net10.0. Microsoft: creating projects
Open the project file and confirm TargetFramework is net10.0. The examples also assume ImplicitUsings and Nullable are enabled. Implicit usings make common .NET names available without repeatedly writing their namespace imports. A namespace groups related names. We will study nullable checking when handling missing text; leave the template's setting enabled now.
Example 1: a complete first program
Replace the contents of Program.cs with:
Console.WriteLine("StudyDeck");
Console.WriteLine("Read one chapter");
Console.WriteLine(15 + 10);
Console.WriteLine("Ready to learn.");Run dotnet run in the project directory. The program output is:
StudyDeck Read one chapter 25 Ready to learn.
Each line is a statement: an instruction in the program. The semicolon ends these statements. Console provides console operations; WriteLine is an operation we call to print a value followed by a line ending. The parentheses contain the value supplied to the call. You will learn to write your own callable operations in the methods lesson.
A string literal is text written between double quotes. The quotes tell the compiler where the text begins and ends; they are not printed. In the third statement, 15 + 10 is an expression: code that produces a value. Here addition produces 25, which is then printed.
These instructions are top-level statements. You do not need to write the program's entry-point method yourself: the compiler supplies the surrounding entry-point structure. This convenience does not mean the instructions are outside the program or bypass compilation. Only one file in a project can contain top-level statements. Microsoft: top-level statements
Editing is not execution
Saving the source file records your edit. Building checks and translates the source into executable .NET output. Running starts the application. dotnet run normally builds as needed before launching, so beginners can use one command during iteration. dotnet build is useful when you want to check compilation without starting the program. Microsoft: running a project
Make one controlled change: replace 10 with 20, save, and predict 35 before rerunning. If you still see 25, check whether you saved the file and ran the intended project. Do not immediately rewrite several lines; that destroys the connection between one change and one observed result.
The build may also print diagnostic messages. Distinguish those tool messages from the lines your program prints. This course's output blocks describe application output, not the entire terminal transcript.
Example 2: text and numbers behave differently
Run this independent replacement program:
Console.Write("Task: ");
Console.WriteLine("Review");
Console.WriteLine("15 + 10");
Console.WriteLine(15 + 10);Output:
Task: Review 15 + 10 25
Write does not append a line ending, so the following WriteLine continues on the same line. Spaces inside quoted text matter: the space after the colon separates the label from Review. The quoted arithmetic is ordinary text; C# does not evaluate instructions hidden inside that string. The unquoted arithmetic is an expression.
The common misconception is that the computer understands what you intended to display. The counterexample above prints both a formula and its result because they are different instructions. Precision is useful: you can inspect exactly which instruction produced an unexpected line.
Read a diagnostic without panic
Try this deliberately broken fragment separately:
Console.WriteLine("StudyDeck")The missing semicolon causes a compilation error. Restore it rather than adding a second project or changing the framework. Another common error is console.WriteLine: C# names are case-sensitive, so that is not the same name as Console.
Read the first relevant diagnostic, find its file and line, inspect nearby punctuation, then rebuild. A later diagnostic may be a consequence of an earlier missing quote or bracket. Compiler wording can vary between SDK versions, so understand the cause instead of memorizing an error sentence.
A program can compile and still be wrong. Printing 15 instead of 25 is not necessarily a compilation failure; the instructions may be valid but fail your requirement. Always compare the actual output with a prediction.
Exercises with solutions
1. Print a two-task plan. Produce exactly three lines: StudyDeck, Read, Practice. Avoid any calculation.
Solution:
Console.WriteLine("StudyDeck");
Console.WriteLine("Read");
Console.WriteLine("Practice");There are three calls and three output lines. The text inside quotes is the content, not a command name.
2. Repair the result. A program prints 8 + 7, but the requirement is to print the number 15. Explain the smallest change.
Solution: Change Console.WriteLine("8 + 7"); to Console.WriteLine(8 + 7);. Removing the quotes changes text into an arithmetic expression. Adding another semicolon would not change the printed value.
Interview questions you can already answer
What is the difference between C# and .NET? C# is the programming language used for these instructions. .NET supplies the runtime, libraries, and development tooling used to build and execute this application.
Does successful compilation prove the output is correct? No. It establishes that the compiler accepted the program under its rules. A requirement such as “show three task names” still needs an output check.
Why can this program run without a visible Main method? Top-level statements let the compiler generate the entry-point structure. There is still an entry point; the syntax simply removes introductory boilerplate.
Before moving on, recreate the four-line program without copying it. Explain the quotes, parentheses, semicolon, and output order aloud. That is stronger evidence of understanding than recognizing the same code on a page.
Analogy
A recipe, a checked instruction sheet, and a kitchen
Imagine writing a short recipe for a kitchen assistant. The source file is your instruction sheet. A compiler checks whether the instructions use the language's allowed forms and prepares an executable version. Running the program is like asking the kitchen to carry out that prepared work.
Changing the sheet does not instantly cook a different meal. You must save the change and use the updated instructions. Likewise, a successful language check cannot prove that you chose the right recipe: “add twenty spoons” may be clear while still being a terrible requirement.
Quoted text resembles a label to print on a container. The label “15 + 10” stays those characters. An actual instruction to add fifteen and ten produces twenty-five instead. This distinction explains why text and expressions give different output.
The analogy breaks because a computer does not interpret a recipe with human judgment. It follows specified language and library behavior. Compilation is also not merely proofreading: it translates the program. Finally, modern .NET execution includes runtime machinery that this kitchen picture deliberately leaves out. Use the picture to remember the stages, then use actual commands and output to verify them.
Quick reference
First-program checklist
- Use a .NET 10 SDK and a console project targeting
net10.0. dotnet new console --framework net10.0 --name StudyDeck.Firstcreates the project.- Edit and save
Program.cs; the.csprojcontains project settings. dotnet buildchecks and builds.dotnet runnormally builds as needed and starts the program.Console.WriteLine("Read");prints text and a line ending.Console.Write("Task: ");leaves the next output on the same line."8 + 7"is text;8 + 7evaluates to 15.- C# names are case-sensitive. Statement-ending semicolons and matching quotes matter.
- Top-level statements have a compiler-generated entry point.
Predict output first. If compilation fails, inspect the first relevant diagnostic. If output is wrong, check your instructions, saved file, and chosen project. Compilation success is not proof that requirements were met.