Loading lessons...
C# Data Types
C# Data Types
A data type specifies what kind of value a variable holds. C# is strongly typed: every variable has a declared type.
The main data types
| Type | Kind | Example |
|---|---|---|
int | whole numbers | int x = 5 |
long | very large whole numbers | long x = 15000000000 |
double | decimal numbers | double x = 5.99 |
float | decimal (smaller precision) | float x = 5.99F |
decimal | high-precision decimal | decimal x = 5.99M |
char | single character | char x = 'A' |
string | sequence of characters | string x = "Hello" |
bool | true or false | bool x = true |
Number types
Use int for most whole numbers. If numbers get very large, use long. For decimals:
float- precision to about 7 digits, suffixF.double- precision to about 15 digits (default for decimals).decimal- precision to 28-29 digits, suffixM, best for money.
float myFloat = 5.75F;
double myDouble = 19.99;
decimal myDecimal = 19.99M;
Text types
char holds exactly one character in single quotes; string holds any text in double quotes.
char grade = 'A';
string name = "Ada";
bool
bool stores true or false - perfect for on/off decisions.
TL;DR
int,longfor whole numbers.float,double,decimalfor decimals.charfor one character,stringfor text.boolfor true/false.