๐ Full lesson with practice questions on DevLadder (free, lesson 3 of 38 in the C# Language: Basics to Advanced course)
| C# keyword | .NET type | Size | Range / precision | Literal |
|---|---|---|---|---|
byte / sbyte
|
Byte / SByte
|
1 byte | 0โ255 / โ128โ127 | 255 |
short / ushort
|
Int16 / UInt16
|
2 bytes | ยฑ32,767 / 0โ65,535 | 100 |
int / uint
|
Int32 / UInt32
|
4 bytes | ยฑ2.1 billion |
42, 42u
|
long / ulong
|
Int64 / UInt64
|
8 bytes | ยฑ9.2 ร 10ยนโธ | 42L |
float |
Single |
4 bytes | ~6โ9 digits, binary floating point | 1.5f |
double |
Double |
8 bytes | ~15โ17 digits, binary floating point (default for decimals) | 1.5 |
decimal |
Decimal |
16 bytes | 28โ29 digits, base-10: use for money | 1.5m |
char |
Char |
2 bytes | One UTF-16 code unit | 'A' |
bool |
Boolean |
1 byte |
true / false
|
true |
string |
String |
reference | Immutable text | "text" |
object |
Object |
reference | Base of every type | : |
Console.WriteLine(0.1 + 0.2 == 0.3); // False: binary floating point can't store 0.1 exactly
Console.WriteLine(0.1m + 0.2m == 0.3m); // True: decimal is exact for base-10 values
int big = 1_000_000; // digit separators
int hex = 0xFF, bin = 0b1010;
// Integer overflow wraps silently by default...
int max = int.MaxValue;
int wrapped = unchecked(max + 1); // -2147483648
// ...unless you ask for checking
try { int boom = checked(max + 1); } catch (OverflowException) { Console.WriteLine("Overflow!"); }
Console.WriteLine(7 / 2); // 3: integer division truncates
Console.WriteLine(7 / 2.0); // 3.5
Console.WriteLine(7 % 2); // 1: remainder
Console.WriteLine(Math.Round(2.5)); // 2: banker's rounding (to even) by default
Console.WriteLine(Math.Round(2.5, MidpointRounding.AwayFromZero)); // 3
Type conversion:
| Kind | Example | Notes |
|---|---|---|
| Implicit (widening) | long l = intValue; double d = intValue; |
Safe, no data loss, no syntax needed |
| Explicit cast (narrowing) |
int i = (int)3.99; โ 3
|
May lose data; truncates decimals |
Convert class |
Convert.ToInt32("42"), Convert.ToInt32(3.5) โ 4
|
Rounds to even; null โ 0 |
Parse |
int.Parse("42") |
Throws FormatException on bad input |
TryParse |
int.TryParse(input, out var n) |
Returns false instead of throwing: use for user input
|
as / is
|
obj as string, obj is int n
|
Reference/pattern conversions without exceptions |
if (int.TryParse(Console.ReadLine(), out int qty) && qty > 0)
Console.WriteLine($"Ordering {qty}");
else
Console.WriteLine("Please enter a positive number");
decimal price = decimal.Parse("1,234.50", NumberStyles.Number, CultureInfo.InvariantCulture); // culture decides what , and . mean
Interview tip: "Why decimal for money?",
doubleis binary and can't represent 0.1 exactly, so sums drift (0.1 + 0.2 != 0.3).decimalstores base-10 digits exactly, at the cost of speed and memory.
Interview questions on C# Language
What is the difference between a value type and a reference type?
A value type variable holds the data itself (int, bool, decimal, DateTime, any struct/enum). Assigning it copies the value. A reference type variable holds a reference to an object on the managed heap (class, string, arrays, delegates, record). Assigning it copies the reference, so two variables can point to the same object.
var a = new List<int>(); var b = a; b.Add(1); // a.Count == 1: same object
int x = 1; int y = x; y++; // x is still 1
Value types are stored inline (stack for locals, inside the parent object for fields); reference-type objects always live on the heap and are cleaned up by the GC.
What is the difference between string and StringBuilder?
string is immutable: every concatenation creates a new string object, so building a string in a loop causes many allocations (O(nยฒ) copying). StringBuilder keeps a mutable buffer and appends in place; call ToString() once at the end. Use StringBuilder for loops or many appends, plain strings/interpolation for a handful of pieces.
Explain const vs readonly vs static readonly.
-
const: compile-time constant, implicitly static, only primitives/strings; the value is baked into callers at compile time. -
readonly: instance field assigned at declaration or in a constructor; can differ per object. -
static readonly: one value per type, computed at runtime (e.g.static readonly HttpClient Client = new()).
Gotcha: if library A has public const int Max = 10 and app B uses it, changing it to 20 in A does nothing in B until B is recompiled. Use static readonly for values that may change.
Read the full lesson with progress tracking and the rest of the course on DevLadder: free step-by-step tutorials and interview questions for developers.
Next in the course: Value types vs reference types โ
Top comments (0)