Lesson 8 +10 XP

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

TypeKindExample
intwhole numbersint x = 5
longvery large whole numberslong x = 15000000000
doubledecimal numbersdouble x = 5.99
floatdecimal (smaller precision)float x = 5.99F
decimalhigh-precision decimaldecimal x = 5.99M
charsingle characterchar x = 'A'
stringsequence of charactersstring x = "Hello"
booltrue or falsebool 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, suffix F.
  • double - precision to about 15 digits (default for decimals).
  • decimal - precision to 28-29 digits, suffix M, 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, long for whole numbers.
  • float, double, decimal for decimals.
  • char for one character, string for text.
  • bool for true/false.