CodeGym /Courses /C# SELF /Getting to Know Variables

Getting to Know Variables

C# SELF
Level 1 , Lesson 3
Available

1. Variables and Boxes

Variables are these special things for storing data. Any kind of data. All data in C# is stored using variables. A variable is basically like a box: just a regular box.

Let's say you write the number 13 on a piece of paper and put that paper in a box. Now we can say that “the box stores the value 13”.

Every variable in C# has three important properties: type, name, and value.

Name is used to tell one variable from another. It's like a label on the box.

The type of a variable defines the kind of values/data you can store in it. Cake goes in a cake box, shoes go in a shoe box, and so on.

Value is just some object, data, or info that's stored in the variable.

Every object in C# has its own type. For example, you can have data types like: integer, floating-point number, text, Cat type, House type, and so on.

Each variable (box) also has its own type. A variable can only store values of the same type as itself. Different boxes are used to store different things: a candy box, an egg carton, etc. Just like in real life.

2. Creating a Variable

In C#, to create a variable, you use a command like this:

type name;
Declaring a variable

Where type is the type of the variable (matches the type of values the variable can store). And name is the variable's name.

Examples:

Creating a variable: first the type, then the name. Description
int a;
We create a variable named a of type int.
string s;
We create a variable named s of type string.
double c;
We create a variable named c of type double.

The two most common types are integers (written as int) and text (written as string). The double type is also popular — that's for floating-point (real) numbers.

3. Assignment

Like we said before, a variable has a name, type, and value. We've already covered name and type, but what about value? How do you put a value into a variable?

To put a value into a variable, there's a special operation — the assignment operation. It copies a value from one variable to another. It doesn't move it, it copies it. Like a file on your drive. Assignment looks like this:

name = value;
Assignment operation

Where name is the variable's name, and value is the value that will be put into the variable. The value can be a literal, another variable's name, or even some expression using variables.

Examples:

Code Description
int i;
int a, b;
int x;
Variable i is created
Variables a, b are created
Variable x is created
i = 3;
The value 3 is put into variable i.
a = 1;
b = a + 1;
The value 1 is put into variable a.
The value 2 is put into variable b.
x = 3;
x = x + 1;
The value 3 is put into variable x.
In the next line, the value of x increases by 1, so x is now 4.

The assignment operation uses the equals sign =. This is not comparison. It's literally a command to copy the value on the right of the equals sign into the variable on the left. For comparison in C#, you use double equals ==.

4. Variable Name: What You Can and Can't Do

Here are some facts you'll want to know when creating variables:

  • The variable name can be anything (for example, temperature, score, userAge).
  • But there are limits — it can't start with a digit, and it can't be the same as a C# keyword (for example, you can't name a variable int or if).
  • It can't have special symbols, except for $ and _.
  • Case matters in the name (Age and age are different variables).
  • Usually, camelCase style is used: first letter is lowercase, then each word starts with a capital (userAge, maxScore).

Examples of allowed names:

int x123 = 1;
string kitten;
double PI = 3.14;
string MAIN_PATH = "c:/";

Examples of forbidden names:

int 1first = 1;			// name can't start with a digit
int number# = 25;		// # symbol can't be used in a name
string name" = "John";	// quotes aren't allowed in a name
double pi+e = 5.123';  	// name can't have +

You'll find out everything else in future lectures. Now a logic question: how many steps does it take to catch a cat in a box?

5. Cats and Boxes

img

How to catch a cat:

  1. Take an empty box.
  2. Wait.

Just kidding 🙂

Well, maybe you can stuff as many cats as you want into a box, but in a variable you can only put one value. And that's what the next task is about.

2
Task
C# SELF, level 1, lesson 3
Locked
The Ultimate Question of the Universe
The Ultimate Question of the Universe
2
Task
C# SELF, level 1, lesson 3
Locked
Finding a Person
Finding a Person
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 38, San Diego, United States
8 August 2025
The answer to the ultimate question of life, the universe, and everything: 42 // Name: Sarah Connor Age: 19 Height: 165.5