Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Variables & Primitive Types · Lesson 23 of 27

Variables

A free lesson from Swipe Coding · C#, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Variables are how your program remembers things — let's give data a name. 📦

A variable is a named container that stores a piece of data your program can work with and change while it runs. In C#, every variable must have a declared type, so the compiler always knows exactly what kind of data it is allowed to hold.

int age = 25;
Console.WriteLine(age);
Declaring an int variable named age, giving it the value 25, and then printing it.
🤖
Notice how this pattern reads almost like a sentence: type, name, value. 🔤

The general pattern for declaring and initializing a variable is: type name = value;. Here, int is the type, age is the name, and 25 is the value assigned with the = operator. You don't have to assign a value right away — you can declare a variable first and give it a value later, as long as you assign something before you try to use it.

🤖
Naming rules feel picky at first, but they make code predictable. 📛

Variable names in C# must start with a letter or an underscore (_) — never a digit — and cannot be one of C#'s reserved keywords (like class or int). Names are case-sensitive, so score and Score are two different variables. By convention, C# developers write variable names in camelCase: the first word is lowercase, and each following word starts with a capital letter, e.g. totalScore or firstName.

Fill in the blank

In C#, variable names conventionally use ____, where the first word is lowercase and every following word starts with a capital letter, such as totalScore.

A variable can also be reassigned: given a new value after it was already initialized. The new value simply replaces the old one — the old value is gone. The new value must still be compatible with the variable's declared type, since C# is statically typed and never lets a variable suddenly hold a different kind of data.

int lives = 3;
lives = lives - 1;
Console.WriteLine(lives); // 2
Reassigning a variable using its own current value is a very common pattern, for example when tracking a changing score or a countdown.
🤖
This shortcut is everywhere in real C# code — good to recognize early. 👀

C# also offers a shortcut called implicit typing with the var keyword: if you initialize a variable right away, C# can figure out its type on its own, so you can write var instead of spelling out the type. The variable still has a fixed, specific type underneath — var is just a convenience for you as the person typing the code.

var age = 25;       // the compiler infers this is an int
var name = "Ada";    // and this is a string
Console.WriteLine(age);
Console.WriteLine(name);
var can only be used when a value is assigned on the same line, since that value is how the compiler figures out the type.
True or false

Once you declare a variable's type, you must write that type again every time you assign it a new value.

Keep your streak going in Swipe Coding

Swipe through every level of C# on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →