C# and .NET • Modern C# baseline
Types, Variables, and Type Inference
Choose types from the data’s meaning and initialize variables before use.
C# is statically typed: the compiler checks operations against declared or inferred types. Use var when the initializer makes the type apparent, not to hide an important distinction. Choose decimal for many decimal financial calculations, and select integer widths based on realistic ranges.
Key points
- var does not make a variable dynamically typed.
- A conversion may lose range or precision; do not assume it is harmless.
Example
int itemCount = 4;
decimal unitPrice = 2.75m;
decimal total = itemCount * unitPrice;
Console.WriteLine(total);
What to expect
11.00
Practice exercise
Add a tax rate and calculate a final total. Keep money values in decimal and state how you round.
Key idea: Types communicate intent and constrain invalid operations at compile time.