C# Data Types
Every C# variable has a data type that decides what it can hold: whole numbers, decimals, money, single characters, text, or true and false.
Novice · C# · Updated
After this lesson you can
- Choose the right C# data type for a value
- Write literals with the right suffix, such as m for decimal
Key takeaways
intcounts whole things,doubleholds measurements,decimalholds money,stringholds text,charone character andbooltrue or false.- Use a
longonly for whole numbers bigger than about 2.1 billion. decimalkeeps money exact to the cent;doublecan store it with tiny errors.- A number with a decimal point is a
doubleunless a suffix says otherwise:mfordecimal,fforfloat.
C# is a strongly typed language: when you declare a variable, you say what type of value it holds, and it can never hold anything else. The type decides which values fit, what you can do with them, and how much memory the variable uses.
A note about comments
You met // comments in the last lesson. C# has two kinds:
// A single-line comment: C# ignores everything after the two slashes
/* A block comment: C# ignores everything
between the opening and the closing marks,
however many lines that is */
Console.WriteLine("Comments are for people, not for C#.");
Comments are for people, not for C#.
The kinds of data
Every C# data type falls into one of these groups:
| Kind of value | Types | Example |
|---|---|---|
| Whole numbers | byte, short, int, long |
a person's age, a number of shots made |
| Floating-point numbers | float, double |
a measurement, a distance, a scientific value |
| Exact decimals, such as money | decimal |
a price, a bank balance |
| A single character | char |
a grade letter: 'A' |
| Text | string |
a name: "Jane" |
| True or false | bool |
is the store open? |
Everything else you build in C# is made from these.
Whole numbers
Programming has several whole-number types. They differ in how big a number they can hold:
| Type | Smallest value | Largest value |
|---|---|---|
byte |
0 | 255 |
short |
-32,768 | 32,767 |
int |
-2,147,483,648 | 2,147,483,647 |
long |
-9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
int is the one you will use most. Choose long only when a value can pass two billion, such as the population of the world.
Each type knows its own limits:
Console.WriteLine("byte: " + byte.MinValue + " to " + byte.MaxValue);
Console.WriteLine("short: " + short.MinValue + " to " + short.MaxValue);
Console.WriteLine("int: " + int.MinValue + " to " + int.MaxValue);
Console.WriteLine("long: " + long.MinValue + " to " + long.MaxValue);
byte: 0 to 255
short: -32768 to 32767
int: -2147483648 to 2147483647
long: -9223372036854775808 to 9223372036854775807
Floating-point numbers
For numbers with a decimal point, C# has float and double. They can hold huge and tiny values, which suits science, games and measurements. double is the one you will use most.
double distanceToMoonInMiles = 238900.5;
Console.WriteLine(distanceToMoonInMiles);
Console.WriteLine(double.MaxValue);
238900.5
1.7976931348623157E+308
E+308 means "times 10 to the power of 308": a 1 followed by 308 zeros.
Money needs decimal
double stores numbers in binary, and many decimal values, such as 1.12, cannot be stored exactly in binary. The tiny errors show up when you add them:
double priceOfCandy = 1.12;
double priceOfSandwich = 2.68;
double totalPrice = priceOfCandy + priceOfSandwich;
Console.WriteLine(totalPrice);
3.8000000000000003
That is not a price you want on a receipt. The decimal type stores decimal digits exactly, so it is the type for money. A decimal value needs an m at the end, so C# knows it is a decimal and not a double:
decimal priceOfCandy = 1.12m;
decimal priceOfSandwich = 2.68m;
decimal totalPrice = priceOfCandy + priceOfSandwich;
Console.WriteLine(totalPrice);
3.80
Use decimal for money and other values that must be exact, and double for measurements and calculations where a tiny rounding error doesn't matter.
Text: char and string
A char holds exactly one character, in single quotes. A string holds any amount of text, in double quotes:
char grade = 'A';
string poet = "Robert Frost";
string poem = "Two roads diverged in a yellow wood";
Console.WriteLine(grade);
Console.WriteLine(poet + " wrote: " + poem);
A
Robert Frost wrote: Two roads diverged in a yellow wood
A char can't do much on its own. Strings can do a great deal, and they get a chapter of their own soon.
True or false: bool
A bool holds the answer to a yes-or-no question: true or false. Name it like a question, so the code reads naturally:
bool isAdult = true;
bool hasDriversLicense = false;
Console.WriteLine("Adult: " + isAdult);
Console.WriteLine("Has a license: " + hasDriversLicense);
Adult: True
Has a license: False
Programs make decisions all the time, so you will use bool constantly.
Literals and their types
A literal is a value written straight into the code, like 12 or "Jose". C# gives each literal a type:
| Literal | Type C# gives it |
|---|---|
12 |
int |
12.5 |
double |
'A' |
char |
"Jose" |
string |
true |
bool |
For the other types, add a suffix to the number:
| Literal | Type |
|---|---|
3000000000L |
long |
12.5f |
float |
12.5m |
decimal |
int age = 35;
long worldPopulation = 8200000000L;
double temperature = 72.5;
float speed = 12.5f;
decimal price = 19.99m;
char letter = 'b';
string name = "Gary";
bool isOnSale = true;
Console.WriteLine(name + " is " + age + ". The price is " + price + ".");
Console.WriteLine("World population: " + worldPopulation);
Console.WriteLine("Temperature: " + temperature + ", speed: " + speed);
Console.WriteLine("Letter: " + letter + ", on sale: " + isOnSale);
Gary is 35. The price is 19.99.
World population: 8200000000
Temperature: 72.5, speed: 12.5
Letter: b, on sale: True
Without the m, decimal price = 19.99; will not compile: 19.99 is a double, and C# won't quietly turn a double into a decimal. The next lesson explains why.
Check yourself
2 questions to try on the lesson page.
So what
Choosing the type is the first decision you make about every value. int for counting, double for measuring, decimal for money, string for text and bool for yes or no will cover almost everything you write in this course.