<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Getting started
All lessons in this course

Lesson 3 of 60 14 minBeginner

Variables and data types

Store values in named boxes, choose the right type for each value, and learn the rules for names, constants and var.

Programs need to remember things: a user’s age, a price, a name. A variable is a named box in memory that holds one value. In C#, every box has a type that decides what can go in it and how much memory it uses.

Program.cs
int age = 25;            // type  name  =  value;string name = "Ravi";double height = 1.75;bool isStudent = true; Console.WriteLine(name + " is " + age);
Output
Ravi is 25

See it in memory

Interactive

Variables are labelled boxes in memory

Pick a type, give it a name and a value, then press Declare. Try putting 300 in a byte!

int score = 90;
int · 4 bytes · Whole numbers from about -2.1 billion to +2.1 billion. The default choice for whole numbers.
Memory
25
ageint
"Ravi"
namestring

The built-in types you will use most

TypeHoldsSizeExample
intWhole numbers (±2.1 billion)4 bytesint count = 10;
longVery large whole numbers8 byteslong views = 9000000000L;
doubleDecimal numbers (15–16 digits)8 bytesdouble pi = 3.14159;
floatDecimals, less precise (7 digits)4 bytesfloat speed = 2.5f;
decimalExact decimals for money16 bytesdecimal price = 199.99m;
booltrue or false1 bytebool isOpen = false;
charOne character2 byteschar grade = 'A';
stringText of any lengthreferencestring city = "Salem";

Declare, assign, change

Program.cs
int score;          // declare (create the box)score = 50;         // assign (put a value in)score = score + 10; // change it: read 50, add 10, store 60Console.WriteLine(score);
Output
60

Naming rules and conventions

  • Must start with a letter or _, then letters, digits or _. No spaces.
  • Cannot be a keyword like int or class.
  • Case-sensitive: age and Age are different variables.
  • Convention: local variables use camelCase: firstName, totalPrice.
  • Use meaningful names: studentCount is far better than sc or x.

var: let the compiler work out the type

Var.cs
var count = 10;        // intvar price = 99.5;      // doublevar name = "Ravi";     // string// count = "ten";      // error: count is still an int!

var is not a “any type” box. The type is fixed at compile time from the value on the right. Use it when the type is obvious from the right-hand side.

Constants

Const.cs
const double Pi = 3.14159;const int MaxStudents = 60;// MaxStudents = 70;   // error: a const can never change

Value types and reference types (first look)

int, double, bool, char and decimal are value types: the box holds the value itself. string is a reference type: the box holds a reference (an address) to the text stored elsewhere, on the heap. You will see why this matters when we study classes and memory.

Quick check

Which type should you use for a bank balance?

Quick check

What is the type of x in var x = 5.0;?

Practice

Create variables for a product: its name (text), price (money), quantity in stock (whole number) and whether it is available (yes/no). Print them on one line each.

Show the solution
Solution.cs
string productName = "Steel Beam";decimal price = 1499.50m;int stock = 12;bool isAvailable = true; Console.WriteLine(productName);Console.WriteLine(price);Console.WriteLine(stock);Console.WriteLine(isAvailable);
Output
Steel Beam
1499.50
12
True

Remember

  • A variable is a named, typed box: type name = value;.
  • Pick the type by the data: int, double, decimal, bool, char, string.
  • Assign before you read; names are camelCase and case-sensitive.
  • var infers a fixed type; const values never change.
0 of 60 lessons completed