C# Basics
C# is a compiled, statically typed language: every variable has a type the compiler knows, so it finds most errors before the program runs. The source becomes IL inside an assembly, and machine code appears at run time, produced by the CLR's JIT compiler. Today the simplest program is a single line, Console.WriteLine("Hello");, with no class and no Main: these are top-level statements, and using System; is supplied by implicit using directives.
This topic is the foundation for all the others. How to declare a variable and why you cannot read it before it is assigned. Which built-in types exist and how double differs from decimal. Why 7 / 2 is 3 while int overflow throws nothing by default. When a conversion happens on its own and when it needs an explicit cast or TryParse. Why a string cannot be changed in place and == compares its contents. The layer-by-layer breakdown is below.
Topic map
- Program structure — the path from
.csto IL and machine code, top-level statements versusstatic void Main, console output, compiler errors and warnings. - Variables — declaration, initialization and assignment, the definite assignment rule, scope,
varandconst. - Built-in types — integral types,
float/double/decimal,bool,char,string; literals and suffixes,MinValue/MaxValue, default values. - Operators — integer division and
%,++/--, short-circuit&&and||, precedence, overflow andchecked,doubleprecision, division by zero. - Type conversions — implicit widening, explicit casts that drop the fraction,
Parse/TryParse,Convert, and the effect of culture. - Strings — indexing and
Length, escape sequences, verbatim and raw literals, interpolation, immutability, and comparison with==.
Common Mistakes and Traps
| Mistake | Consequence |
|---|---|
Expecting 3.5 from 7 / 2 | Both operands are int, so the division is integral and the result is 3; at least one double is needed: 7.0 / 2 |
Assuming int guards against overflow | Arithmetic is unchecked by default: int.MaxValue + 1 gives -2147483648 with no exception; checked turns the check on |
Storing money in double | 0.1 + 0.2 == 0.3 is False; money and exact decimal fractions call for decimal |
| Reading a local before assigning it | Compile error CS0165: unlike fields, locals get no default value |
Expecting (int)3.99 to round | A cast drops the fraction: 3, and (int)-3.99 gives -3 |
Parsing input with int.Parse | The string "4x" throws FormatException; input that may be wrong calls for int.TryParse |
Thinking s.ToUpper() changes s | Strings are immutable: the method returns a new string, which is lost without an assignment |
Writing console.WriteLine | C# is case-sensitive: error CS0103, the name console does not exist |
Interview relevance
The basics are rarely asked as a separate block, but "what does this code print" questions are built on them. The interviewer checks your model, not the syntax: do you know the operands decide the result type, that int overflows silently, that double is a binary fraction and that a string is immutable.
Typical checks:
- The result of
7 / 2,-7 / 2,-7 % 3, and why. - What
int.MaxValue + 1does, and the difference betweencheckedandunchecked. - When to use
decimalversusdouble, and why0.1 + 0.2 != 0.3. - The difference between
(int)x,Convert.ToInt32(x)andint.Parse(s), and whyTryParseexists. - What
varis and why it is not dynamic typing. - Why a string is immutable and what
==compares for strings.
Common wrong answer: "double is fine for money, we'll just round on output." That opens the discussion that double stores a binary fraction in which 0.1 is inexact, errors accumulate with every addition, while decimal stores the number in base 10 and counts cents exactly.