CSharp Basics

Lessons C# Book Chapter 2
C# Book · Chapter 2

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.

Chapter 2 of 6

Variables: Giving Things Names

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.

int starCount = 50;
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.


Numbers and Math

C# handles math exactly how you expect:

int x = 10 + 5; // 15
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#.


Lists & Arrays: Buckets of Stuff

C# has two common ways to store collections of values: arrays and lists.

string[] colors = { “red”, “cyan”, “hotpink”, “yellow” };
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.


Loops: Doing Things Over and Over

A loop runs the same block of code multiple times. The most common one you’ll see is the for loop:

for (int i = 0; i < 50; i++)
{
    CreateStar();
}

Change 50 to 2000 and you spawn 2000 stars. Everything inside the braces runs once per loop.


Methods: Teaching C# New Tricks

A method is a reusable block of code you give a name to:

static Star CreateStar()
{
    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.


Your First 30‑Second C# Test

Let’s make sure everything works before we build anything bigger.

  1. Open Visual Studio.
  2. Create a new Console App.
  3. Replace the contents of Program.cs with:
Console.WriteLine(“My C# Clubhouse is officially open!”);
  1. Press F5 to run it.

If you see:

My C# Clubhouse is officially open!

Everything is working perfectly. You’re ready for your first real C# project.

You’re in C# Book · Chapter 2 of 6
Play‑First Programming · playfirstprogramming.com A clubhouse for curious builders