top of page

C# for Beginners. Lessons 1: VARIABLES part 3

  • Writer: Pavel Zosim
    Pavel Zosim
  • 2 hours ago
  • 2 min read

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:




💻 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