C# variables have an explicit type: int count = 0;. Unlike JavaScript, you cannot silently change a variable from int to string without a compile error. var still infers a fixed type at compile time—like auto in C++.
Common scalar types
int,long— integersdouble,float,decimal— floating point (decimalfor money)char— single UTF-16 code unitbool—trueorfalsestring— immutable text (reference type on the heap)
var and initialization
var count = 10; // inferred as int
double ratio = 0.75;
bool ok = true;
string name = "Ada";
Value types live on the stack or inline in structs; reference types like string point to heap objects managed by the GC—contrast with manual memory in C++.
Important interview questions and answers
- Q: var vs dynamic?
A:varis compile-time type inference;dynamicdefers binding to runtime (rare in typical C#). - Q: Value type vs reference type?
A: Value types copy by value; reference types copy a reference—mutations through one reference affect the shared object.
Self-check
- What type does var infer from an integer literal?
- Is string a value type or reference type?
Pitfall: var still picks a fixed type at compile time—unlike dynamic typing in JavaScript.
Interview prep
- What does var do?
Infers a fixed compile-time type from the initializer—still statically typed, not dynamic.
- Value vs reference types?
Structs and primitives live on the stack or inline; classes are reference types allocated on the managed heap.