C# tutorials > Core C# Fundamentals > Basics and Syntax > What are the primitive data types in C#?
What are the primitive data types in C#?
In C#, primitive data types are the fundamental building blocks for storing data. They are predefined types provided by the C# language itself. Understanding these types is crucial for writing efficient and reliable code. They directly represent data values and are not composed of other types. They're also sometimes referred to as built-in types.
Overview of Primitive Data Types
C# provides a rich set of primitive data types, each designed to store specific kinds of values. These can be broadly categorized into:
Integral Types
Integral types are used to store whole numbers. C# offers several integral types, each with a different range of values:
byte age = 30; // Stores an unsigned 8-bit integer (0 to 255)
sbyte temperature = -10; // Stores a signed 8-bit integer (-128 to 127)
short year = 2023; // Stores a signed 16-bit integer (-32,768 to 32,767)
ushort port = 8080; // Stores an unsigned 16-bit integer (0 to 65,535)
int count = 1000; // Stores a signed 32-bit integer (-2,147,483,648 to 2,147,483,647)
uint population = 5000000; // Stores an unsigned 32-bit integer (0 to 4,294,967,295)
long bigNumber = 1234567890123; // Stores a signed 64-bit integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
ulong veryBigNumber = 9876543210987654321; // Stores an unsigned 64-bit integer (0 to 18,446,744,073,709,551,615)
Floating-Point Types
Floating-point types are used to store numbers with fractional parts:
float price = 99.99f; // Stores a single-precision floating-point number
double pi = 3.14159265359; // Stores a double-precision floating-point number
Decimal Type
The decimal
type is specifically designed for financial and monetary calculations where high precision is essential. It uses a 128-bit data type.
decimal salary = 50000.50m; // Stores a high-precision decimal number
Boolean Type
The bool
type represents logical values, which can be either true
or false
.
bool isLoggedIn = true; // Stores a boolean value (true or false)
Character Type
The char
type is used to store a single Unicode character. It is a 16-bit value.
char initial = 'J'; // Stores a single Unicode character
Memory Footprint
Each primitive type consumes a specific amount of memory. The size (in bytes) varies based on the type: Understanding the memory footprint helps in optimizing memory usage, especially when dealing with large datasets.
byte
: 1 bytesbyte
: 1 byteshort
: 2 bytesushort
: 2 bytesint
: 4 bytesuint
: 4 byteslong
: 8 bytesulong
: 8 bytesfloat
: 4 bytesdouble
: 8 bytesdecimal
: 16 bytesbool
: Typically 1 byte (implementation-dependent)char
: 2 bytes
When to Use Them
Choosing the right data type is crucial for efficient and accurate code:
int
for most integer values unless you have a specific need for a smaller or larger range.double
for general-purpose floating-point numbers.decimal
for financial calculations to avoid rounding errors.bool
for representing true/false conditions.char
for storing single characters.byte
when memory optimization is critical and the value range is within 0-255 (e.g., representing pixel color components).
Best Practices
Here are some best practices when working with primitive data types:
decimal
for financial calculations to ensure accuracy.
FAQ
-
What is the difference between `float` and `double`?
float
is a single-precision floating-point type (32-bit), whiledouble
is a double-precision floating-point type (64-bit).double
offers more precision and a wider range thanfloat
.double
is generally the default choice unless you have specific memory constraints. -
Why use `decimal` instead of `double` for monetary values?
Thedecimal
type provides higher precision and avoids rounding errors that can occur withdouble
when representing decimal fractions. This makes it suitable for financial and monetary calculations where accuracy is paramount. -
What happens if I assign a value larger than the maximum value of an `int`?
If you assign a value larger than the maximum value of anint
, an overflow exception may occur, or the value might wrap around to a negative number (depending on compile-time vs. runtime behavior and compiler settings). It's important to validate input and choose a data type that can accommodate the expected range of values. -
What are the signed and unsigned integer?
Signed integers (e.g.,int
,short
) can represent both positive and negative numbers, while unsigned integers (e.g.,uint
,ushort
) can only represent non-negative numbers (zero and positive values). Unsigned integers effectively double the maximum positive value that can be stored compared to their signed counterparts, by not using half of the value range to store negative numbers.