C# for Beginners. Lessons 1: VARIABLES part 2
- Pavel Zosim
- 2 hours ago
- 5 min read
uint - Unsigned 32-bit Integer
Beginner explanation: Like int, but only positive numbers (0 to 4.3 billion). Perfect for bit operations, color values (RGBA), large positive numbers, hash codes, or anything that should never be negative. Double the positive range of int!
📊 Technical Specs:
Size: 4 bytes (32 bits)
Range: 0 to 4,294,967,295 (~4.3 billion)
Signed: No (unsigned - positive only)
Default Value: 0
⚡ Performance Notes:
✅ Same speed as int (32-bit optimized)
✅ Double positive range vs int
✅ Perfect for bit operations (no sign bit)
⚠️ DANGER: Subtraction wrapping is a common source of bugs
💡 Use for: bit flags, colors, hashes, frame counters
💡 Avoid for: anything involving subtraction or comparisons with negative values
When to use uint:
✅ Bit masks and flags (32 boolean values in one variable)
✅ Packed color values (RGBA format)
✅ Hash codes and checksums
✅ Frame/tick counters (never negative)
✅ Large positive-only IDs
✅ Network packet data
❌ Don't use for loops that count down
❌ Don't use if you ever subtract or compare with negatives
❌ Don't use just because "it's never negative" - int is safer!
💻 Syntax & Declaration:
🎮 Practical Use Cases:
⚠️ Common Pitfalls:
💡 Pro Tips:
long ★ Large Numbers & Timestamps ★
Beginner explanation: A very big box for very big numbers! Can store from -9 quintillion to +9 quintillion. Use for timestamps (milliseconds since 1970), file sizes (gigabytes), large calculations, database IDs, or when int isn't big enough. Takes double the memory of int, but you get HUGE range.
📊 Technical Specs:
Size: 8 bytes (64 bits)
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (~±9.2 quintillion)
Signed: Yes (can be negative)
Default Value: 0
⚡ Performance Notes:
✅ Fast on 64-bit systems (native register size)
⚠️ Slightly slower on 32-bit systems (requires two operations)
✅ Same speed as int on modern 64-bit CPUs
✅ No conversion overhead when working with time APIs
💡 Uses 2x memory of int (8 bytes vs 4 bytes)
💡 Use when: int range isn't enough OR working with timestamps/file sizes
When to use long:
✅ Timestamps (milliseconds since Unix epoch)
✅ File sizes (especially > 2GB)
✅ Database IDs (primary keys)
✅ High-precision timers
✅ Large world coordinates (space games)
✅ When calculations might exceed ±2 billion
❌ Don't use by default (wastes memory)
❌ Don't use for small counters/IDs
💻 Syntax & Declaration:
🎮 Practical Use Cases:
⚠️ Common Pitfalls:
💡 Pro Tips:
ulong - Unsigned 64-bit Integer
Beginner explanation: The biggest box for positive numbers only! Can store 0 to 18.4 quintillion (18 followed by 18 zeros!). Use for extremely large positive values like file sizes, crypto operations, huge counters, or scientific calculations. Rarely needed in typical game development, but essential for specific cases.
📊 Technical Specs:
Size: 8 bytes (64 bits)
Range: 0 to 18,446,744,073,709,551,615 (~18.4 quintillion)
Signed: No (unsigned - positive only)
Default Value: 0
⚡ Performance Notes:
✅ Same speed as long on 64-bit systems
⚠️ Slower on 32-bit systems
✅ Double positive range vs long
⚠️ Subtraction wrapping is dangerous (like uint)
💡 Very rarely needed in game development
💡 Use only when: long range isn't enough AND values are positive-only
When to use ulong:
✅ Extremely large file sizes (> 9 exabytes)
✅ Cryptographic operations (hashing, keys)
✅ Scientific calculations with huge positive values
✅ 64-bit hashing
✅ Blockchain/cryptocurrency data
✅ Astronomical calculations
❌ Almost never needed in typical games
❌ Don't use for countdown loops
❌ Don't use if long is sufficient
💻 Syntax & Declaration:
🎮 Practical Use Cases:
⚠️ Common Pitfalls:
💡 Pro Tips:
4. FLOATING POINT TYPES (Decimal Numbers)
These types handle numbers with fractional parts (decimals). Critical for physics, positions, and any calculations requiring precision.
float ★ Unity/Unreal Standard ★
Beginner explanation: The standard way to store decimal numbers in game engines (Unity/Unreal). Can store numbers like 3.14, -0.5, or 1000.75. Has about 6-7 digits of precision, which is enough for most game physics, positions, and rotations. ALWAYS add 'f' after the number (like 5.0f) to tell the compiler it's a float!
📊 Technical Specs:
Size: 4 bytes (32 bits)
Precision: ~6-7 significant digits
Range: ±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸
Default Value: 0.0f
⚡ Performance Notes:
✅ Fast - hardware accelerated on all platforms
✅ GPU prefers float (Vector/Matrix math)
✅ Half memory of double (4 vs 8 bytes)
✅ Unity/Unreal use float as standard
⚠️ ~6-7 digits precision (errors accumulate!)
⚠️ Large + small numbers lose small value
💡 Unity standard - use float everywhere in Unity
💡 Performance: Faster than double on some platforms
When to use float:
✅ Unity/Unreal game development (always!)
✅ 3D positions, rotations, scales
✅ Physics (velocity, acceleration, forces)
✅ Game timers, cooldowns
✅ UI values (alpha, scale, position)
✅ Audio (volume, pitch, pan)
❌ Don't use for money/currency (precision errors!)
❌ Don't use for exact equality comparisons
💻 Syntax & Declaration:
🎮 Practical Use Cases:
⚠️ Common Pitfalls:
💡 Pro Tips:
double ★ C# Math Standard ★
Beginner explanation: More precise version of float with ~15-16 digits of accuracy. The default decimal type in C# (when you write 5.5 without 'f', it's a double). Use for scientific calculations, math libraries, or when you need more precision than float. Slower and uses more memory than float, but much more accurate.
📊 Technical Specs:
Size: 8 bytes (64 bits)
Precision: ~15-16 significant digits
Range: ±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸
Default Value: 0.0
⚡ Performance Notes:
✅ Fast on 64-bit systems (native register size)
⚠️ Slower than float on some platforms (~10-30% slower)
⚠️ Uses 2x memory of float (8 vs 4 bytes)
✅ Math class uses double (no conversion overhead)
✅ Better precision for scientific calculations
💡 C# standard for math operations
💡 Unity: Stick with float; convert from double only when needed
When to use double:
✅ Scientific calculations
✅ Math library operations (Math.Sin, Math.Sqrt, etc.)
✅ High-precision requirements (astronomy, physics simulations)
✅ Statistical analysis
✅ General C# programming (non-Unity)
❌ Don't use in Unity (use float instead)
❌ Don't use for money (use decimal!)
❌ Don't use in performance-critical loops if float is sufficient
💻 Syntax & Declaration:
🎮 Practical Use Cases:
⚠️ Common Pitfalls:
💡 Pro Tips:
...read C# for Beginners. Lessons 1: VARIABLES part 2
Like this post? ( ´◔ ω◔`) ノシ
Support: Buy Me a Coffee | Patreon | GitHub | Gumroad | YouTube








Comments