TExcelTAD
Home › C# › C# Data Types

C# Data Types

Every value in C# has a type, which decides how much memory it uses, which values it can hold and which operations are allowed on it. Choosing the right type avoids overflow bugs and rounding surprises.

Built-in types at a glance

Type Size Range / precision Literal example
byte 1 byte 0 to 255 200
short 2 bytes -32,768 to 32,767 1000
int 4 bytes about ±2.1 billion 42
long 8 bytes about ±9.2 quintillion 42L
float 4 bytes ~6–9 significant digits 1.5f
double 8 bytes ~15–17 significant digits 1.5
decimal 16 bytes 28–29 significant digits, base 10 1.5m
bool 1 byte true / false true
char 2 bytes One UTF-16 character 'x'
string varies Any sequence of characters "text"

Each numeric type exposes its limits through MinValue and MaxValue:

csharp · try it yourself
using System;

Console.WriteLine(
quot;byte: {byte.MinValue} to {byte.MaxValue}"); Console.WriteLine(
quot;short: {short.MinValue} to {short.MaxValue}"); Console.WriteLine(
quot;int: {int.MinValue} to {int.MaxValue}"); Console.WriteLine(
quot;long: {long.MinValue} to {long.MaxValue}");
byte:  0 to 255
short: -32768 to 32767
int:   -2147483648 to 2147483647
long:  -9223372036854775808 to 9223372036854775807

Integer types

Use int by default for whole numbers. Switch to long when values may exceed about two billion — for example file sizes in bytes or milliseconds since 1970. Unsigned variants (uint, ulong, ushort) exist for special cases.

Large literals can use the _ digit separator for readability, and hexadecimal (0x) or binary (0b) notation:

csharp · try it yourself
using System;

long worldPopulation = 8_100_000_000L;
int mask = 0b_1111_0000;
int color = 0xFF8800;

Console.WriteLine(worldPopulation);
Console.WriteLine(mask);
Console.WriteLine(color);
8100000000
240
16746496

Floating-point types: float, double and decimal

double is the default for fractional numbers and is fast, but it stores values in binary, so many decimal fractions cannot be represented exactly. decimal stores numbers in base 10 with high precision — use it for money.

csharp · try it yourself
using System;

double d = 0.1 + 0.2;
decimal m = 0.1m + 0.2m;
float f = 1.0f / 3;

Console.WriteLine(d == 0.3);
Console.WriteLine(d);
Console.WriteLine(m == 0.3m);
Console.WriteLine(m);
Console.WriteLine(f);
False
0.30000000000000004
True
0.3
0.33333334

Note the suffixes: f makes a float literal and m a decimal literal. Without a suffix, 1.5 is a double, and assigning it to a float or decimal variable does not compile.

Tip: Never compare double results with == after arithmetic. Check whether the difference is tiny instead: Math.Abs(a - b) < 1e-9.

Characters, booleans and strings

A char holds a single UTF-16 code unit in single quotes; a string is an immutable sequence of characters in double quotes; a bool is true or false.

csharp · try it yourself
using System;

char letter = 'C';
bool isValid = false;
string language = "C#";

Console.WriteLine(letter);
Console.WriteLine((int)letter);
Console.WriteLine(isValid);
Console.WriteLine(language.Length);
C
67
False
2

Value types and reference types

C# types fall into two families:

Value types Reference types
Examples int, double, bool, char, decimal, structs, enums string, arrays, classes, records
Variable holds The value itself A reference (address) to an object
Assignment copies The value The reference — both variables see the same object
Can be null? Only as int? etc. (nullable) Yes

You will meet this distinction again in the Classes and Structs lessons.

Test yourself

Which type should you use for amounts of money?

  1. float
  2. double
  3. decimal
  4. int

What is the type of the literal 2.5f?

  1. double
  2. float
  3. decimal
  4. string

Which of these is a reference type?

  1. int
  2. bool
  3. string
  4. char