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, andbool.
-
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
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 likeisDeadorhasKey.
csharp
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
7. Visual Learning: Variable Memory Map
txt
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 aredouble(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. Open your Visual Studio Console project.
-
2.
In the
Mainfunction, write the following:
csharp
- 3. Press Play. Watch the math process and output the updated player profile!
11. Practice Exercises
- 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.
-
2.
What is the shorthand code to increase an integer variable named
scoreby 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, afloat, and adoublein C#. Why do game engines primarily usefloatfor 3D coordinates instead ofdouble?
- 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 declareint 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.