C# Introduction: Understand Your First Program
C# lets you turn information and rules into a program that a computer can run. Start by understanding what the program does and checking its result—not by memorizing every keyword at once.
What you will understand
- Distinguish the C# language from the .NET platform and SDK.
- Follow source code through compilation and execution.
- Read a small console program and predict its output.
- Separate text, numbers, calculations, and input assumptions.
- Repair a valid program that produces the wrong answer.
What is C#?
C# is a programming language, pronounced C sharp. A programming language gives you a precise way to describe information, calculations, and decisions. Instead of manually subtracting a workshop's bookings from its capacity, you can write the rule once and ask a program to apply it to different values.
C# is commonly used with .NET to build web applications, APIs, desktop software, services, and other kinds of applications. Different application types need different tools and libraries. Learning the language gives you foundations; it does not mean you already know every web or mobile framework.
We begin with console applications: programs that write text to a terminal. A console gives us a small, visible result without first building buttons, screens, or a database. The same habits—name your information, describe the rule, check the result—remain useful in larger applications.
C#, C++, and Java are different languages. They share some familiar-looking punctuation, but their tools, standard libraries, and program rules are not interchangeable. A Java example cannot become C# simply by changing the filename.
C#, .NET, and the SDK have different jobs
C# is the language you write. .NET is the platform that provides a runtime and libraries for applications. The SDK, or software development kit, adds the tools used to create, build, and run projects. Installing only a runtime is not the same as installing the SDK needed to compile your own program.
A source file such as Program.cs contains the instructions you edit. A project file such as Lesson.csproj describes how the application should be built. Our browser runner supplies the framework references and compiler settings for you; the editor contains only your C# source.
During a typical .NET build, the C# compiler checks the source and produces an assembly containing intermediate instructions and metadata. The .NET runtime loads the application and, in the normal execution model used here, translates methods into machine code as needed. There are other publishing models, including ahead-of-time compilation; you do not need them for this lesson.
Keep the practical distinction simple: the editor changes source, the compiler checks and builds it, and the runtime executes the built application. Output is what the running program produces. A source file is not its own output.
- WriteEdit Program.cs
- BuildThe compiler checks and builds an assembly
- Run.NET executes it and produces output
Read your first program before changing it
Read the two messages below and predict their order. Then select Run code. The service compiles the current editor contents and runs the compiled program in an isolated .NET container. The expected output is a separate reference, not a simulated execution result.
The using System directive lets this file refer to the Console type by its short name rather than writing System.Console. The dot in Console.WriteLine selects a method on Console. Parentheses contain the argument supplied to the method, and the semicolon ends the statement.
These are top-level statements. Modern C# allows the compiler to supply the entry-point structure around them. You can focus on the work the program performs before learning how to design classes and methods. Keep the using line intact and change only a message on your first attempt.
Program.cs · Your first two messages
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Welcome to C# practice.
Read, predict, run, and explain.Each WriteLine call prints its argument and then ends the line. Execution reaches the first call before the second. The quotation marks identify text in the source; they are not printed as part of the message.
Text that looks like arithmetic is still text
A string is text. Quotation marks tell the compiler where that text begins and ends. The program prints the content rather than treating every word or symbol inside it as an instruction. That is why printing a sentence containing a plus sign does not perform an addition.
Compare all three statements before running them. In the interpolated message, the number is calculated rather than typed directly into the label. If you change an operand, predict both calculated results again. Editing an expected-output reference would not change the executable program.
Program.cs · Text is not a calculation
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
12 + 3
15
Total: 15The first line prints the characters inside the quotes. The second evaluates an addition. The third uses an interpolated string: the expression inside braces is evaluated and its result is inserted into the message. The dollar sign introduces interpolation; without it, braces would be ordinary text.
Follow information through a useful calculation
A workshop has 30 seats and 18 bookings. Independently calculate the answer: 30 minus 18 leaves 12. This small manual prediction gives you something meaningful to compare with the program. A successful run alone does not prove that the rule is correct.
The keyword int declares a whole-number value. The equals sign assigns the value on its right to the name on its left; here it is not an instruction to compare two values. The availableSeats statement uses the values already assigned to totalSeats and bookedSeats.
Change bookedSeats to 20. The new answer should be 10. The names describe the values' roles, not an automatic connection to a live booking service. Both inputs are written into the source, and no person supplies input while this program runs.
This first version assumes that bookings are between zero and the total capacity. With 32 bookings, it prints -2. That tells you the input is outside the assumed domain; the example does not yet reject overbooking. Validation and decisions are later topics, so do not mistake an assumption for an implemented check.
Program.cs · Workshop places
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Workshop places
Available seats: 12First, the program stores two whole-number values. It subtracts booked seats from total seats and stores that result under a third name. Finally, it inserts the result into a labelled message. The program does not know what a workshop is: the subtraction implements the rule you chose.
Why some examples contain class Program and Main
You will also encounter the explicit entry-point form below. It performs the same two print operations as the first program. Main is where this version starts; the class groups the method. For now, recognize the wrapper without trying to design your own use of every keyword.
Use either the top-level form or the explicit Main form for one example. Do not paste both complete programs into the same editor and expect both entry points to run. In a project, top-level statements belong to a single source file.
Replace the editor contents when trying a different complete example. This also prevents duplicate local names from unrelated examples from becoming confusing compilation errors. The filename Program.cs is a helpful project convention; it is not a rule requiring every C# class to have that exact name.
Program.cs · The explicit entry-point form
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Welcome to C# practice.
Read, predict, run, and explain.This complete alternative places the same operations inside Main, a static entry-point method in a class. Its braces group the method body and class body. You do not need this extra structure for today's top-level examples, but recognizing it makes older tutorials and existing projects less confusing. Run each form separately, not pasted together.
Optional: run the same programs locally
Install the .NET 10 SDK if you want to practise locally. Create a new project in a new practice folder with the commands above. Replace the generated Program.cs contents with one complete example, save it, then run dotnet run from the folder containing CSharpFirstSteps.csproj. The command builds first when needed, then executes the application. Setup and build messages are separate from the program output shown in this lesson. You do not need a local SDK to use the page's configured browser runner.
dotnet --version
dotnet new console --name CSharpFirstSteps
cd CSharpFirstStepsNow replace Program.cs with one complete example and save it. Inside the project folder, run:
dotnet runChange one message and run again. Seeing that change helps confirm you are editing and running the same project.
Predict and repair a believable mistake
The workshop has 30 seats and 18 bookings, but this program says 48 seats are available. Predict the current result, find the incorrect rule, and repair only the calculation. Explain why compilation succeeds even though the answer is wrong.
Program.cs · Find the incorrect calculation
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Available seats: 48This is an intentional logic error, not a compilation error. The compiler can verify that adding two integers is legal, but it cannot infer that your business rule requires subtracting bookings from capacity.
Reveal the repair and explanation
Program.cs · Explained repair
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Available seats: 12Replace addition with subtraction because available capacity is total capacity minus already booked capacity. With no bookings the answer is 30; with 30 bookings it is zero. Those boundary cases help you check the rule independently of the original example.
- With zero bookings, expect 30 available seats.
- With 18 bookings, expect 12 available seats.
- With 30 bookings, expect zero available seats.
- With 32 bookings, observe -2 and explain the missing validation rather than claiming it is handled.
Independent lab: a reading tracker
Build a small console reading tracker. A book named The Quiet Library has 120 pages, of which 45 have been read. Calculate the remaining pages and print a labelled three-line summary. Work from the starter before opening the reference solution.
Acceptance checklist
- Store the book title as text and both page counts as whole numbers.
- Calculate pagesLeft from totalPages and pagesRead; do not hard-code 75.
- Print the book title, pages read, and pages left on separate labelled lines.
- Keep the example limited to reporting: no keyboard input, files, or database are required.
- State the assumption that pagesRead is between zero and totalPages.
Program.cs · Reading tracker starter
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Book: Choose a title
Pages read: 0
Pages left: 0The starter compiles, but it does not yet implement the intended reading calculation. Replace the sample title and pages-read value, then calculate the remaining pages. Do not simply replace pagesLeft with the expected answer: it must respond to changed inputs.
Need a hint?
- First solve the arithmetic without code: 120 minus 45.
- The workshop example already contains the operation you need.
- Use an interpolated string when inserting a named value into a label.
Reveal the reference solution
Program.cs · Reading tracker reference solution
Predict the result, edit Program.cs, then run your program.
Standard input · optional
Select Run code to compile and execute this program.
Book: The Quiet Library
Pages read: 45
Pages left: 75The title is text, while page counts are whole numbers. Calculating pagesLeft from totalPages and pagesRead keeps the rule connected to its inputs. This is a console report, not a saved reading app: it does not read a file, accept keyboard input, or remember progress after the program ends.
Test changes, not just the starter values
- pagesRead = 0 should leave 120 pages.
- pagesRead = 45 should leave 75 pages.
- pagesRead = 120 should leave zero pages.
- pagesRead = 130 produces -10: explain why this version does not validate progress.
- Change totalPages to 200 and pagesRead to 80; expect 120 remaining pages.
Common errors and how to investigate them
The compiler cannot find Console or WriteLine
C# names are case-sensitive: Console.WriteLine is not console.writeline. Keep using System; at the top of these examples. Read the reported line and correct the spelling before changing unrelated code.
A semicolon or quotation mark is missing
Check the statement before the reported error as well as the highlighted line. A missing closing quote can make later text look like part of the string. Use straight quotation marks in source, not typographic quotes copied from prose.
The program runs but gives the wrong number
Write down the input values and the intended calculation. Compare addition with subtraction and check whether a number was placed inside quotes. Legal C# can still implement the wrong rule.
The dotnet command is not found locally
The SDK may be missing or unavailable on your terminal's PATH. Install the SDK, reopen the terminal, and check dotnet --version. This says nothing about the validity of the program's source.
Run code reports that the sandbox is unavailable
That is an execution-infrastructure issue, not proof of a C# syntax error. Local preview needs Docker and the configured .NET SDK image. There is no fallback that runs learner code directly on the website host. Production execution requires a separately configured service.
Questions beginners ask
Is C# the same as .NET?
No. C# is the language. .NET supplies a runtime and libraries, and the .NET SDK supplies development tools. The names are related but describe different parts of the workflow.
Do I need to know Java or C++ first?
No. This lesson begins with ordinary text and whole-number calculations. Previous programming experience can help, but similar-looking names and braces do not make those languages interchangeable.
Must I write class Program and Main?
Not for the modern top-level programs used here. The compiler supplies the entry-point structure. The explicit form is included so you can recognize it in other projects; both complete examples are runnable separately.
Does the workshop example accept real bookings?
No. Its two inputs are fixed values in the source. It calculates and prints a result; it does not accept bookings, read keyboard input, save information, or prevent overbooking.
Will my editor changes be saved?
The runner does not persist edits across a refresh. Copy any code you want to keep. Source and optional standard input are sent to this site's execution endpoint only when you select Run code; do not include secrets.
Can you explain your first program?
Answer every question, then check the explanations. A correct prediction matters more than memorizing a keyword.
Recap and next step
C# describes your program; the SDK builds it, and the .NET runtime runs it. A compiler checks language rules, while your predictions and test cases check the intended behavior. You have read complete programs, changed their inputs, and repaired a calculation without pretending that a console report is a full application.
Continue to SDK and editor setup using Next lesson below. You will install the tools, create a project, and verify your own save-build-run workflow.
References and further reading
These are original SovranCode explanations, scenarios, exercises, and quiz questions. Technical details were checked against Microsoft’s documentation.
Last updated: