LLearnCode
Academy/csharp/Read and write small programs

beginner · 22 minute study

A declared type is a contract for every assignment

Read a C# program without treating its extra syntax as decoration. Understand which mistakes are caught before execution and which remain your responsibility.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

First, build a mental model

A variable has a type even when you let the compiler infer it. C# source is compiled for .NET; a browser does not execute that source as JavaScript.

  1. int stores whole numbers; bool stores a decision; string stores text. Choose the representation for the operations you need.
  2. var means infer a static type from this initializer. It does not mean the type can change on the next line.
  3. Two int operands use integer division: 5 / 2 is 2. Introduce an appropriate fractional operand when the contract needs a fraction.
  4. decimal represents base-10 values with a finite range and precision; the m suffix selects a decimal literal. Rounding rules still need an explicit business decision.
Read the codecsharp · educational example
int score = 10;
var bonus = 3; // bonus has type int
score += bonus;
int half = 5 / 2;
decimal price = 12.50m;
decimal total = price * 2;
Console.WriteLine(score);
Console.WriteLine(half);
  1. Bind

    score and bonus are both int. The inferred type of bonus stays int.

  2. Calculate

    score becomes 13; integer division makes half equal 2.

  3. Validate

    The compiler checks type compatibility. It does not decide whether half was the intended business result.

Production thinking

Take this beyond the example.

Use C# for a deliberate runtime and deployment target. Compare it with JavaScript's dynamic runtime and TypeScript's erased checks; do not assume the same numeric semantics across languages.

Sources and further reading

Your evidence

unseen is a local learning signal, not a credential or proof of production competence.

Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.

Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.

Next lesson · Guard invalid cases before repeating useful work →