top of page

C# for Beginners. Lessons 1: VARIABLES part 3

  • Writer: Pavel Zosim
    Pavel Zosim
  • Dec 6, 2025
  • 3 min read

Updated: Dec 7, 2025


decimal  Money & Financial Calculations ★

Beginner explanation: The MOST precise decimal type, designed specifically for money and financial calculations. Has ~28-29 digits of precision and avoids the rounding errors that plague float/double. ALWAYS use decimal for currency, prices, and financial data. Much slower than float/double, so NEVER use in game loops or real-time calculations. Requires 'm' or 'M' suffix!


📊 Technical Specs:

  • Size: 16 bytes (128 bits)

  • Precision: ~28-29 significant digits

  • Range: ±1.0 × 10⁻²⁸ to ±7.9228 × 10²⁸

  • Default Value: 0.0m


⚡ Performance Notes:

  • VERY SLOW - 10-100x slower than float/double

  • ❌ No hardware acceleration (software-emulated)

  • ❌ Uses 4x memory of float (16 vs 4 bytes)

  • EXACT - no rounding errors for decimal values

  • ✅ Perfect for financial calculations

  • 💡 NEVER use in Update()/FixedUpdate() - too slow!

  • 💡 Use ONLY for money, prices, currency


When to use decimal:

  • ✅ Money/Currency (prices, costs, balances)

  • ✅ Financial calculations (interest, tax, exchange rates)

  • ✅ Accounting/billing systems

  • ✅ Shop UI (display prices)

  • ✅ Any calculation where exact decimal precision is legally required

  • ❌ NEVER in game loops (Update, FixedUpdate)

  • ❌ NEVER for positions, physics, transforms

  • ❌ NEVER for real-time calculations

  • ❌ Don't use if float/double are sufficient


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:



char - Single Character

Beginner explanation: Stores a single character like 'A', '5', '@', or '中'. Uses single quotes, not double quotes! Internally it's just a number (0-65535) representing a Unicode character. Perfect for keyboard input, single letters, or text parsing.


📊 Technical Specs:

  • Size: 2 bytes (16 bits)

  • Range: '\u0000' to '\uffff' (0 to 65,535 - Unicode characters)

  • Default Value: '\0' (null character)


⚡ Performance Notes:

  • ✅ Fast (just a 16-bit integer internally)

  • ✅ 2 bytes (same as ushort)

  • ✅ Good for menu systems, keyboard input

  • 💡 String is usually more practical for text

  • 💡 Use char for single-character operations only


When to use char:

  • ✅ Single character storage

  • ✅ Keyboard input (single key)

  • ✅ Menu options (A, B, C, D)

  • ✅ Character-by-character text parsing

  • ✅ Grades, ratings (A, B, C)

  • ✅ Direction indicators (N, S, E, W)

  • ❌ Don't use for multi-character text (use string)

  • ❌ Emojis need string (surrogate pairs)


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:


string - Text Sequences

Beginner explanation: Stores text of any length - from empty "" to entire novels! Uses double quotes. Strings are IMMUTABLE (can't be changed after creation) - modifying a string actually creates a new one in memory. Use for names, messages, UI text, file paths - anything textual.


📊 Technical Specs:

  • Size: Dynamic (depends on text length)

  • Type: Reference type (stored on heap, not stack)

  • Immutable: Once created, cannot be modified

  • Default Value: null


⚡ Performance Notes:

  • ⚠️ Immutable - every modification creates new string object

  • ❌ Concatenation in loops is VERY slow (use StringBuilder)

  • ✅ String interpolation ($"") is optimized by compiler

  • ✅ String comparison is fast (interned strings)

  • 💡 Use StringBuilder for 10+ concatenations

  • 💡 Memory: Stores on heap (garbage collected)


When to use string:

  • ✅ Player names, item names, descriptions

  • ✅ UI text, dialogue, messages

  • ✅ File paths, URLs

  • ✅ JSON, XML, serialized data

  • ✅ User input, commands

  • ❌ Don't concatenate in loops (use StringBuilder)

  • ❌ Don't use for single characters (use char)


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:


bool - True/False Values

Beginner explanation: Stores only two values: true or false. Essential for game logic, conditions, and states. Think of it as an on/off switch, yes/no answer, or enabled/disabled flag. Uses 1 byte of memory despite being just 1 bit of information.


📊 Technical Specs:

  • Size: 1 byte (8 bits) - but logically 1 bit

  • Values: true or false only

  • Default Value: false


⚡ Performance Notes:

  • ✅ Very fast (1 byte, simple operations)

  • ✅ Comparison operators are optimized

  • ✅ Short-circuit evaluation saves CPU

  • 💡 Despite being 1 bit logically, uses 1 byte (memory alignment)

  • 💡 For thousands of flags, consider bit packing


When to use bool:

  • ✅ Any yes/no, true/false, on/off state

  • ✅ Conditions and flags

  • ✅ Ability checks (canJump, canAttack)

  • ✅ State tracking (isAlive, isPaused)

  • ✅ UI visibility (showMenu, isEnabled)

  • ❌ Don't use int (0/1) instead of bool

  • ❌ Don't use for multi-state (use enum instead)


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:




Like this post? ( ´◔ ω◔`) ノシ

Support: Buy Me a Coffee | Patreon | GitHub | Gumroad YouTube 

Comments


bottom of page