Skip to main content
C# for Games – Complete Beginner to Advanced Guide
CHAPTER 02 Beginner

C# Syntax and Fundamentals

Updated: May 16, 2026
20 min read

# CHAPTER 2

C# Syntax and Fundamentals

1. Introduction

Every language—whether it is English, Japanese, or C#—has rules. Grammar dictates how sentences are structured. In programming, this grammar is called Syntax. If you break the syntax rules, the computer throws a "Compiler Error" and refuses to run the game. In this chapter, we will master the absolute fundamentals of C# syntax. We will learn how to store game data in memory using Variables, define what kind of data we are storing using Data Types, manipulate that data using Math Operators, and leave helpful notes in our code using Comments.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • End C# statements correctly with a semicolon ;.
  • Declare and initialize Variables to store game data.
  • Understand core Data Types: int, float, string, and bool.
  • Perform mathematical operations (+, -, *, /).
  • Use line and block comments to document your code.

3. The Semicolon (The Period of C#)

In English, a sentence ends with a period. In C#, a command ends with a semicolon (;). If you forget the semicolon, the compiler will panic and crash.
csharp
12
Console.WriteLine("Hello"); // Correct
Console.WriteLine("World")  // ERROR! Missing semicolon

4. Variables and Data Types

A Variable is a box in the computer's memory where we store data. Before we create the box, we must declare what *type* of data it is allowed to hold.
  • int (Integer): Whole numbers. Used for ammo, level, or score.
  • float: Decimal numbers. Used for speed, movement, or exact positioning. *Must end with an 'f'.*
  • string: Text. Used for player names or dialogue.
  • bool (Boolean): True or False. Used for states like isDead or hasKey.
csharp
12345
// Syntax: DataType variableName = value;
int ammo = 30;
float walkSpeed = 5.5f;
string enemyName = "Goblin";
bool isPoisoned = false;

5. Math Operators

Games are driven by math. When a player takes damage, we subtract. When they collect a coin, we add.
  • + (Add), - (Subtract), * (Multiply), / (Divide)
  • Shorthand Operators: Gamers constantly modify variables. Instead of writing ammo = ammo - 1;, C# provides shortcuts:
  • ammo -= 1; (Subtracts 1)
  • ammo--; (Exactly the same, decrements by exactly 1)
  • score += 100; (Adds 100 to the current score)

6. Comments (Leaving Notes)

Code can get complicated. You can write notes in plain English that the computer will completely ignore.
  • Single-line comment: Use //
  • Multi-line comment: Use /* to start and */ to end.
csharp
1234567
// This subtracts health from the player
health -= 20; 

/* 
  TODO: Add a particle explosion here later.
  Make sure the sound effect plays first.
*/

7. Visual Learning: Variable Memory Map

txt
12345
[ COMPUTER MEMORY (RAM) ]
+-------------------+-------------------+-------------------+
| int playerLevel   | float jumpForce   | bool isJumping    |
| [ 5 ]             | [ 12.5f ]         | [ true ]          |
+-------------------+-------------------+-------------------+

8. Best Practices

  • Camel Case Naming: Always name your variables using camelCase. The first word is lowercase, and every subsequent word is Capitalized. Do not use spaces.
  • Good: maxHealth, playerMovementSpeed
  • Bad: Maxhealth, player movement speed, pms

9. Common Mistakes

  • Forgetting the 'f' on Floats: If you type float speed = 5.5;, C# will throw an error. By default, C# assumes all decimals are double (a massive data type). You must explicitly append an 'f' (5.5f) to tell C# it is a lightweight float, which is the standard for game engines like Unity.

10. Mini Project: The Player Profile

Objective: Declare various data types and print them together (String Concatenation).
  1. 1. Open your Visual Studio Console project.
  1. 2. In the Main function, write the following:
csharp
1234567891011121314151617181920212223
using System;

class Program
{
    static void Main()
    {
        // 1. Declare Variables
        string heroName = "Arthur";
        int level = 5;
        float gold = 150.5f;
        bool hasSword = true;

        // 2. Perform Math
        level++; // Arthur leveled up!
        gold += 50.0f; // Arthur found gold!

        // 3. Output to Console (Concatenation)
        Console.WriteLine("Hero: " + heroName);
        Console.WriteLine("Level: " + level);
        Console.WriteLine("Gold: " + gold);
        Console.WriteLine("Equipped Sword: " + hasSword);
    }
}
  1. 3. Press Play. Watch the math process and output the updated player profile!

11. Practice Exercises

  1. 1. Write a line of code to declare a whole-number variable representing the number of lives a player has, and set it to 3.
  1. 2. What is the shorthand code to increase an integer variable named score by exactly 10 points?

12. MCQs with Answers

Question 1

Which C# data type is specifically designed to hold a simple true or false value, making it perfect for tracking if a door is locked or if a player is grounded?

Question 2

Why will the following line of code fail to compile in C#? int playerHealth = 100

13. Interview Questions

  • Q: Explain the difference between an int, a float, and a double in C#. Why do game engines primarily use float for 3D coordinates instead of double?
  • Q: What is variable initialization, and what happens if you try to use a variable in C# that has been declared but not initialized?
  • Q: Walk me through the concept of "Strongly Typed" languages versus "Dynamically Typed" languages. Why is C# considered strongly typed?

14. FAQs

Q: Can I change a variable's data type later? A: No. Because C# is strongly typed, if you declare int score = 0;, you cannot later say score = "Hello";. The compiler will block it, which actually prevents hundreds of bugs from occurring during gameplay.

15. Summary

In Chapter 2, we learned the grammar of C#. We discovered that every command must end with a semicolon. We learned how to carve out specific chunks of memory using Data Types (int, float, string, bool) and named those chunks using camelCase Variables. Finally, we learned how to manipulate our game state using Math Operators and how to document our logic using Comments. We can now store and alter game data.

16. Next Chapter Recommendation

Our code currently runs top-to-bottom, executing every single line. But what if we only want the player to jump *IF* they are standing on the ground? Proceed to Chapter 3: Conditions and Loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·