C# Basics — Just Enough to Play
You could skip this chapter entirely, paste the AI’s code into Visual Studio, press Run, and have a working C# project in five minutes. But C# has more structure than HTML or Python — and knowing a few core ideas will make your knob‑twisting far more fun.
This chapter covers exactly five things. Nothing more.
A variable is a name you attach to a piece of information so you can use it later. In C#, you must also tell the computer what *type* of information it is.
double speed = 2.5;
string color = “cyan”;
When you tweak knobs in our projects, you’ll almost always be looking for variables like these — named values near the top of the file that control one specific behavior.
C# handles math exactly how you expect:
int y = 100 – 30; // 70
int z = 4 * 25; // 100
double speed = 10.0 / 4.0; // 2.5
The // symbol starts a comment — notes for humans, ignored by C#.
C# has two common ways to store collections of values: arrays and lists.
List<double> speeds = new List<double> { 1.0, 2.5, 0.8, 3.2 };
When the AI spawns 50 objects, it will usually store them in a list — one list of objects, each with its own position, speed, and color.
A loop runs the same block of code multiple times. The most common one you’ll see is the for loop:
{
CreateStar();
}
Change 50 to 2000 and you spawn 2000 stars. Everything inside the braces runs once per loop.
A method is a reusable block of code you give a name to:
{
int x = Random.Shared.Next(0, 800);
int y = Random.Shared.Next(0, 600);
double speed = Random.Shared.NextDouble() * 2;
return new Star(x, y, speed);
}
You rarely need to write methods from scratch — but knowing what they are helps you find the right one to tweak.
Let’s make sure everything works before we build anything bigger.
- Open Visual Studio.
- Create a new Console App.
- Replace the contents of
Program.cswith:
- Press F5 to run it.
If you see:
Everything is working perfectly. You’re ready for your first real C# project.