Chapter roadmap
Understand the .NET execution model and write strongly typed console programs with variables, conversions, calculations, and validated input. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Explain source, IL, and runtime execution
- Use C# types and nullable values
- Format output
- Parse and validate console input
01.1
C# and the .NET runtime
C# source is compiled into Intermediate Language and metadata. The .NET runtime loads that code and uses just-in-time compilation for the current platform. A project file records target framework, dependencies, and build settings, while Program.cs contains the application entry point.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
ExampleConsole.WriteLine("Hello from C#");ResultHello from C#
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.2
Variables, types, and inference
C# is statically typed. int, double, decimal, bool, char, string, and DateTime model common values. var asks the compiler to infer a type from an initializer; it does not make the variable dynamically typed. Use decimal for base-10 financial calculations.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplestring course = "C#";
int lessons = 6;
decimal price = 19.95m;
bool available = true;
var message = $"{course}: {lessons} lessons, {price:C}";
Console.WriteLine(message);ResultC#: 6 lessons, $19.95
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.3
Nullable values and conversion
Value types normally require a value, while a nullable value type such as int? may also be null. Use explicit conversion methods and TryParse for uncertain text. The null-coalescing operator ?? supplies a fallback without confusing missing data with zero.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
ExampleConsole.Write("Age: ");
string? raw = Console.ReadLine();
if (int.TryParse(raw, out int age) && age >= 0)
{
Console.WriteLine($"Next year: {age + 1}");
}
else
{
Console.WriteLine("Enter a valid nonnegative age.");
}ResultAge: 16
Next year: 17
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.4
Operators and formatted output
Arithmetic, comparison, logical, and assignment operators resemble Java and C++. Integer division drops the fractional part, so convert or use a decimal operand when a decimal result is required. Interpolated strings combine expressions with optional format specifiers.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampledouble earned = 43;
double possible = 50;
double percent = earned / possible;
Console.WriteLine($"Score: {percent:P1}");ResultScore: 86.0%
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A1.1
Create a profile report using at least seven suitable types and interpolated output.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.2
Build a safe two-number calculator using TryParse and protected division.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.3
Calculate subtotal, tax, discount, and total with decimal and currency formatting.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.4
Parse a date and report the day of week and days remaining, handling invalid input.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Console Intake Report
Console Intake Report โ Collect a person's contact and measurement data, validate every field, calculate at least four derived values, and print a formatted report. Use nullable annotations intentionally and never allow malformed input to crash the program.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- .NET
- runtime
- IL
- static type
- var
- nullable
- TryParse
- interpolation
- format specifier