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
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.
varlets 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, notporx.
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:
- a data type: what kind of value it holds, such as a whole number or some text
- a name: how you refer to it
- 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 =.
// 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);
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:
int numberOfCars = 3;
Console.WriteLine(numberOfCars);
3
Printing text and values together
Console.WriteLine can print some text followed by a value. Join them with +:
int numberOfCars = 3;
Console.WriteLine("The number of cars at my house: " + numberOfCars);
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:
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);
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
intorclass
These names are legal:
name
firstName
age
numberOfCars
_lastName
column5
These are not, and the code will not compile:
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:
firstName
ageOfOldestChild
pricePerGallon
numberOfChocolateChipCookies
totalYardSize
Names that make a reader guess:
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.
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.