Advertisement
Beginner C# Lesson 2 of 5

Lesson 2: Data Types in C#

C# is a strongly typed language — every variable must have a declared type, and that type determines what values it can store and what operations can be performed on it. In this lesson, we'll explore all the built-in data types in C# and when to use each one.

Advertisement

Value Types vs Reference Types

C# types fall into two fundamental categories:

📦 Value Types

Store data directly in memory (stack). Copying creates a new independent value.

int, double, bool, char, float, decimal, struct, enum

🔗 Reference Types

Store a reference (pointer) to data on the heap. Copying shares the same underlying data.

string, object, array, class, interface, delegate

Integer Types

Use integer types for whole numbers. The main difference is the range and memory size:

TypeSizeMin ValueMax ValueUse When
byte1 byte0255Small positive numbers, image data
short2 bytes-32,76832,767Small integers, memory-sensitive code
int4 bytes-2.1 billion2.1 billionDefault choice for whole numbers
long8 bytes-9.2 quintillion9.2 quintillionVery large numbers, file sizes, IDs
uint4 bytes04.2 billionWhen negatives are impossible
int age = 25;
long worldPopulation = 8_000_000_000L; // L suffix for long literal
byte redChannel = 255;
short temperature = -40;

// Check the max value of int
Console.WriteLine(int.MaxValue);   // 2147483647
Console.WriteLine(long.MaxValue);  // 9223372036854775807

Floating-Point Types

For numbers with decimal points, C# offers three options:

float
System.Single · 4 bytes
±1.5×10⁻⁴⁵ to ±3.4×10³⁸ · ~7 digits precision
double
System.Double · 8 bytes
±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸ · ~15 digits precision
decimal
System.Decimal · 16 bytes
±1.0×10⁻²⁸ to ±7.9×10²⁸ · 28–29 digits precision
float   temperature = 98.6f;      // f suffix required
double  pi          = 3.14159265; // default for decimals
decimal price       = 19.99m;     // m suffix required, use for money

Console.WriteLine(price * 3);  // Output: 59.97

💡 Rule of thumb: Use decimal for money and financial calculations (exact precision). Use double for scientific/general math. Avoid float unless memory is very constrained.

Boolean Type

The bool type stores only true or false. It's used in conditions and flags:

bool isLoggedIn = true;
bool hasPermission = false;
bool isAdult = age >= 18;  // result of comparison is bool

if (isLoggedIn && hasPermission)
{
    Console.WriteLine("Access granted!");
}

Character Type

The char type stores a single Unicode character. Use single quotes:

char grade  = 'A';
char symbol = '@';
char newline = '\n';  // escape sequences work too

// char can be used as a number (Unicode code point)
Console.WriteLine((int)'A');  // Output: 65
Advertisement

String Type

The string type is a sequence of characters. Unlike char, it uses double quotes and is a reference type:

string greeting   = "Hello, World!";
string empty      = "";               // empty string
string nullStr    = null;             // reference can be null

// String interpolation (recommended)
string name = "Ravi";
int score = 95;
Console.WriteLine($"Student {name} scored {score}%");

// Verbatim string — no escaping needed
string path = @"C:\Users\Ravi\Documents";

The object Type

object is the base type for all C# types. Any value can be assigned to an object variable, but you lose type safety and performance (boxing/unboxing). Use it sparingly.

object anything = 42;
anything = "now a string";
anything = true;

// To use the value, you must cast it back:
int num = (int)anything; // ❌ would fail — anything is now bool

🧠 Quick Check — Lesson 2

Which C# type should you use to store a product price accurately (e.g., $19.99)?

Lesson Summary

C# has value types (stored on stack) and reference types (stored on heap).

Use int for whole numbers, double for decimals, decimal for money.

bool stores true or false; char stores one character (single quotes); string stores text (double quotes).

Float literals need an f suffix; decimal literals need an m suffix; long literals need an L suffix.

object can hold any value but should be used sparingly due to performance overhead.

Up Next

Lesson 3: Type Conversion & Casting — implicit, explicit, and Convert class

Next Lesson →