top of page

C# for Beginners. Lessons 1: VARIABLES part 1

  • Writer: Pavel Zosim
    Pavel Zosim
  • Nov 23
  • 6 min read

Updated: 17 minutes ago

by Pavel Zosim • edit v.1.0

Learn about data types, variable declarations, and arithmetic operations with optimization tips

1. Introduction

If you've never programmed before but want to start making games in Unity - this guide is for you! I'll explain everything as simply as possible, without complex terminology.


This guide provides a comprehensive overview of C# data types and operations for Unity developers. Whether you're new to programming or transitioning from visual scripting, this reference covers fundamental concepts with practical examples and performance tips.


🎮 What are Variables?

Imagine you're playing a game. Your character has:

  • Health (a number from 0 to 100)

  • Name (text: "Hero")

  • Is alive (yes or no)

  • Position in the world (X, Y, Z coordinates)


The computer needs to store this information somewhere in memory, and it needs to know what type of information you're storing to work with it correctly. That's what variables and data types are for!


Simple analogy: Think of variables as labeled boxes where you store different things. The label tells you what can go inside - you wouldn't put milk in a toolbox or screws in a milk carton, right? Same principle!


variables types

2. Understanding Variables

Every variable declaration in C# follows a consistent pattern:



Syntax breakdown:

  • int - Data type (specifies what kind of information)

  • health - Variable name (identifier you choose)

  • = 100 - Assignment operator and value

  • ; - Semicolon terminates the statement (always needed!)


📦 Three Key Concepts

Before we dive into specific types, understand these core concepts:


a) Size: Memory footprint measured in bytes. Determines how much RAM the variable occupies.

  • Example: byte uses 1 byte, int uses 4 bytes, long uses 8 bytes

  • Why it matters: If you're making a mobile game with thousands of enemies, using byte instead of int for health could save significant memory!

b) Range: Minimum and maximum values the variable can store.

Exceeding this causes overflow.

  • Example: byte can only store 0 to 255. If you try to store 256, it wraps back to 0!

  • Why it matters: Choosing the right range prevents bugs and saves memory

c) Default Value: Initial value assigned automatically if not explicitly initialized.

  • Numbers default to 0, booleans to false, strings to null

  • Best practice: Always initialize your variables explicitly to avoid confusion!


📝 C# Data Types:


Decision Guide:

For whole numbers:

  • Use int by default (99% of cases)

  • Use byte for 0-255 range in large arrays (health, colors)

  • Use long only if exceeding ±2 billion

For decimal numbers:

  • Use float in Unity/Unreal (engine standard)

  • Use double for general C# math

  • Use decimal ONLY for money (never in game loops!)

For text:

  • Use char for single character

  • Use string for text of any length

For true/false:

  • Use bool

Now let's explore the most important types in detail.

3. INTEGER TYPES (Whole Numbers)


int ★ YOUR DEFAULT INTEGER TYPE ★

Beginner explanation: The most common way to store whole numbers (no decimals) in C#. Use this for counting things like score, enemies, level, coins - anything that doesn't need fractions. Think of it as your "standard box" for numbers.


📊 Technical Specs:

  • Size: 4 bytes (32 bits)

  • Range: -2,147,483,648 to 2,147,483,647 (±2.1 billion)

  • Signed: Yes (can be negative)

  • Default Value: 0


⚡ Performance Notes:

  • Fastest integer type on all platforms (32-bit and 64-bit)

  • ✅ Modern CPUs optimized for 32-bit operations

  • ✅ No conversion overhead when used with standard libraries

  • ✅ Cache-friendly (fits perfectly in CPU registers)

  • ⚠️ Division is slow (~10-40 CPU cycles) - avoid in tight loops if possible

  • 💡 Use by default unless you have specific memory/range requirements


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls


💡 Pro Tips



bite - Small Positive Numbers (0-255) -

Beginner explanation: A tiny box that can only hold small positive numbers from 0 to 255. Perfect for things like health bars (0-100 HP), percentages (0-100%), or color values (red, green, blue are each 0-255). Can't store negative numbers or anything bigger than 255.


When to use byte:

  • ✅ Health bars (0-100)

  • ✅ RGB color values (0-255)

  • ✅ Percentages (0-100)

  • ✅ Small counters that never exceed 255

  • ✅ Large arrays of small values

  • ❌ Don't use for values that might exceed 255

  • ❌ Don't use when you need negative numbers


📊 Technical Specs:

  • Size: 1 byte (8 bits)

  • Range: 0 to 255

  • Signed: No (unsigned - positive only)

  • Default Value: 0


⚡ Performance Notes:

  • Smallest integer type - saves 75% memory vs int

  • ✅ Perfect for large arrays (textures, heightmaps, voxel data)

  • ✅ GPU-friendly (Color32 in Unity uses bytes)

  • ⚠️ Arithmetic operations convert to int (slight overhead)

  • ⚠️ Range checks needed for game logic (health, damage)

  • 💡 Use in large arrays or when range is naturally 0-255

  • 💡 Memory matters: 1000 bytes = 1KB, 1000 ints = 4KB


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:



sbite - Signed 8-bit Integer -

Beginner explanation: Like byte, but can go negative. Stores numbers from -128 to +127. Useful for small values that need both positive and negative (like temperature in Celsius, small offsets, or directional values). Rarely used in games because byte or int are usually better choices.


When to use sbyte:

  • ✅ Small values needing negatives (-128 to 127)

  • ✅ Directional input (-1, 0, 1)

  • ✅ Small deltas/offsets

  • ✅ 8-bit audio samples

  • ✅ Temperature values (if limited range)

  • ❌ Rarely used in modern game development

  • ❌ Usually better to use int instead


📊 Technical Specs:

  • Size: 1 byte (8 bits)

  • Range: -128 to 127

  • Signed: Yes (can be negative)

  • Default Value: 0


⚡ Performance Notes:

  • ✅ Same size as byte (1 byte)

  • ✅ Good for small arrays needing negative values

  • ⚠️ Arithmetic operations convert to int (overhead)

  • ⚠️ Very limited range compared to int

  • 💡 Rarely used in practice - byte or int are more common

  • 💡 Most game engines don't use sbyte - check documentation


💻 Syntax & Use Cases:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:



short - Signed 16-bit Integer -

Beginner explanation: Medium-sized box for whole numbers. Can store from -32,768 to +32,767. Good for coordinates on small maps, item IDs in smaller games, or counters that might exceed 255 but won't reach millions. Bigger than byte, smaller than int.


📊 Technical Specs:

  • Size: 2 bytes (16 bits)

  • Range: -32,768 to 32,767

  • Signed: Yes (can be negative)

  • Default Value: 0


⚡ Performance Notes:

  • ✅ Uses 50% memory of int

  • ✅ Good for large arrays needing negative values

  • ⚠️ Arithmetic operations convert to int (overhead)

  • ⚠️ Limited range - often better to use int

  • 💡 Use when: range fits AND memory matters

  • 💡 Modern practice: int is usually preferred (CPU optimized for 32-bit)


When to use short:

  • ✅ Small map coordinates (maps < 32K × 32K)

  • ✅ Memory optimization in large arrays

  • ✅ 16-bit audio samples

  • ✅ Item/Entity IDs in smaller games (< 32K items)

  • ✅ Building heights, floor numbers

  • ❌ Don't use for large maps (overflow!)

  • ❌ Modern CPUs prefer int (32-bit optimized)


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:


ushort - Unsigned 16-bit Integer

Beginner explanation: Like short, but only positive numbers (0 to 65,535). Double the positive range! Great for network port numbers, character codes (Unicode), small object IDs, or anything needing 0-65K range without negatives.


📊 Technical Specs:

  • Size: 2 bytes (16 bits)

  • Range: 0 to 65,535

  • Signed: No (unsigned - positive only)

  • Default Value: 0


⚡ Performance Notes:

  • ✅ Same memory as short (2 bytes)

  • ✅ Double positive range vs short

  • ⚠️ Arithmetic operations convert to int

  • ⚠️ Subtraction can wrap around (dangerous!)

  • 💡 Use for: ports, Unicode, IDs up to 65K

  • 💡 Less common than byte/int in game development


When to use ushort:

  • ✅ Network port numbers (0-65535)

  • ✅ Unicode character codes

  • ✅ Object IDs up to 65K

  • ✅ Voxel/block types (Minecraft-style)

  • ✅ Audio sample rate values

  • ✅ Small positive-only counters

  • ❌ Don't use if you need negatives

  • ❌ Be careful with subtraction (wrapping!)


💻 Syntax & Declaration:


🎮 Practical Use Cases:


⚠️ Common Pitfalls:


💡 Pro Tips:



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

Comments


bottom of page