# What is a Variable

A variable is a named place where your program remembers a value, so it can use it again later.

Novice · C# · Updated November 15, 2026

By Gregor Dzierzon

## After this lesson you can

- Declare a variable, give it a value and print it
- Choose a legal, meaningful variable name

## Key takeaways

- A variable is a named place that holds a value: declare it with a type and a name, then give it a value.
- Giving a variable a new value replaces the old one, so it holds only its latest value.
- `var` lets C# work out the type from the value you give the variable.
- Names use letters, digits and `_`, can't start with a digit, and are written in camelCase.
- Choose names that say what the value means, such as `pricePerGallon`, not `p` or `x`.

A **variable** is a way for a program to remember a small piece of information. You give the value a name, and from then on the program can use that name to read the value, change it, or calculate with it.

Every variable has three parts:

1. a **data type**: what kind of value it holds, such as a whole number or some text
2. a **name**: how you refer to it
3. a **value**: what it holds right now

## Declaring a variable

**Declaring** a variable creates it: you write the data type, then the name. **Assigning** puts a value in it, with `=`.

```csharp
// The data type is int (a whole number) and the name is numberOfCars
int numberOfCars;

// Put the value 3 into numberOfCars
numberOfCars = 3;

Console.WriteLine(numberOfCars);
```

```text
3
```

`Console.WriteLine` prints the value the variable holds, not its name. Lines that start with `//` are **comments**: notes for people reading the code, which C# ignores.

**Try it.** In your `csharp-basics` folder, make a file named `siblings.cs`. Declare a variable named `numberOfSiblings`, set it to how many brothers and sisters you have, print it, and run the file with `dotnet run siblings.cs`.

## Declaring and assigning in one line

When you already know the value, you can declare the variable and assign it in one line:

```csharp
int numberOfCars = 3;
Console.WriteLine(numberOfCars);
```

```text
3
```

## Printing text and values together

`Console.WriteLine` can print some text followed by a value. Join them with `+`:

```csharp
int numberOfCars = 3;
Console.WriteLine("The number of cars at my house: " + numberOfCars);
```

```text
The number of cars at my house: 3
```

The space at the end of the text matters: without it, the 3 would sit right against the colon.

**Try it.** Declare a variable named `workHours` that holds how many hours you work in a day. Print `Hours I work in a day: ` followed by the value, all on one line.

## Letting C# work out the type with var

Instead of writing the data type, you can write `var`. C# looks at the value and works out the type for you:

```csharp
var numberOfCars = 3;         // C# sees a whole number, so this is an int
var city = "Salt Lake City";  // C# sees text in quotes, so this is a string

Console.WriteLine(numberOfCars);
Console.WriteLine(city);
```

```text
3
Salt Lake City
```

The variable still has a type. `numberOfCars` is an `int` exactly as if you had written `int`, and it can only ever hold whole numbers. `var` only saves typing. While you are learning, writing the type yourself helps you notice which type each value is, so this course writes types out in its early lessons.

## Naming rules

C# has strict rules for variable names. A name:

- can contain letters, digits and `_` (the underscore)
- must start with a letter or `_`
- cannot contain spaces or other symbols
- cannot be a word C# already uses, such as `int` or `class`

These names are legal:

```text
name
firstName
age
numberOfCars
_lastName
column5
```

These are not, and the code will not compile:

```text
first name    (a space)
first-name    (a hyphen)
last*name     (a symbol)
1stName       (starts with a digit)
class         (a word C# already uses)
```

## Naming conventions

Beyond the rules, programmers follow **conventions**: habits that make code easier to read. These are the ways names are commonly written:

- **camelCase**: starts lowercase, and each new word starts with a capital: `numberOfCars`
- **PascalCase**: every word starts with a capital: `NumberOfCars`
- **snake_case**: all lowercase, words joined by underscores: `number_of_cars`
- **UPPER_CASE**: all capitals, words joined by underscores: `NUMBER_OF_CARS`

In C#, **variables are written in camelCase**. You will use PascalCase later for the names of classes, methods and properties.

## Choose meaningful names

A good name says what the value means. Someone reading your code, including you next month, should not have to guess.

Good names:

```text
firstName
ageOfOldestChild
pricePerGallon
numberOfChocolateChipCookies
totalYardSize
```

Names that make a reader guess:

```text
x         (x of what?)
first     (first name? first place? first pet?)
price     (the price of what?)
cookies   (how many cookies? which kind?)
column1   (what is in the column?)
```

Single-letter names have a few accepted uses, such as counting in a loop, which you will see soon. Everywhere else, spell out what the value is.

## Do it: show the kitchen what's in the oven

The kitchen at Build-It Pizzeria has a screen that shows two facts: how many pizzas are in the oven, and whose order comes next. Your program keeps each fact in a variable, then prints it.

The two variables:

- `pizzasInOven`: how many pizzas are in the oven, an `int`. Right now it's `4`.
- `nextCustomer`: the name of the next customer, a `string`. Right now it's `"Maria"`.

With those values, the program prints exactly these two lines:

```text
Pizzas in the oven: 4
Next customer: Maria
```

**Your job:** write the program that stores the two facts and prints them.

- Declare an `int` named `pizzasInOven` and give it the value `4`.
- Declare a `string` named `nextCustomer` and give it the value `"Maria"`.
- Print `Pizzas in the oven: ` (with a space after the colon) followed by the value of `pizzasInOven`.
- On the next line, print `Next customer: ` (with a space after the colon) followed by the value of `nextCustomer`.
- Join the text and the variable with `+`, so the values come from the variables.

Until both variables exist, the Tests tab says the tests use something your code doesn't have yet. That's expected.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

Variables are how a program remembers. Every program you write from here on starts by putting values into well-named variables, then working with them.

## Key terms

- [Variable](https://www.ask-codey.com/glossary/variable-c-sharp)
- [Declaration](https://www.ask-codey.com/glossary/declaration-c-sharp)
- [Assignment](https://www.ask-codey.com/glossary/assignment-c-sharp)
- [Comment](https://www.ask-codey.com/glossary/comment-c-sharp)
- [var](https://www.ask-codey.com/glossary/var-c-sharp)
- [camelCase](https://www.ask-codey.com/glossary/camelcase-c-sharp)

## Cheat sheets

- [What is a Variable](https://www.ask-codey.com/cheat-sheets/variables-in-csharp-what-is-a-variable-cheat-sheet)

Source: https://www.ask-codey.com/lessons/variables-in-csharp-what-is-a-variable
