C# Value Types Tutorial: Complete Guide for Beginners
C# value types hold data directly in memory. They are faster and work well for simple values like numbers and characters.
imagine you’re organizing a marathon race. You need to track details like:
- Runner’s bib number (whole number) →
int
- Distance covered (decimal) →
float
ordouble
- Time taken (decimal) →
double
- Did the runner finish? (yes/no) →
bool
Since all of these are simple data types that store their actual values directly in memory, they are Value Types!
Why Are These Value Types?
- ✅ Stored directly in memory → Fast & efficient!
✅ Independent copies → If you copy a value type, the original doesn’t change.
✅ Great for small, fixed-size data like numbers, booleans, and characters.
Your fitness tracker or smartwatch probably stores your distance, heart rate, and time using value types because they are simple and fast to process!
Common Data Types (Value Type)
-
Numeric Data Types
-
Boolean Type
-
Character Data Type
1. Numeric Data Types
Numeric data types store numbers. There are different types of numbers, so C# provides a data type for each one. Here are the main types:
int
: Stores whole numbers, like1
,50
, or-10
.long
: Stores larger whole numbers, like1000000
or5000000000
.float
: Stores decimal numbers, but with less precision (e.g.,3.14
).double
: Stores decimal numbers with more precision (e.g.,3.14159
).decimal
: Best for money or high-precision calculations (e.g.,19.99m
).
Integer Data Type
Storing whole numbers, from zero to billions, with ease.
An integer (int
) in C# is a whole number. It can be positive, negative, or zero, like 10
, -5
, or 1000.
- ✅ Stores whole numbers
✅ Usesint
keyword
✅ No decimals allowed
✅ Example values:1, -10, 500
Integer is useful when counting things, storing IDs, or doing simple math.
Example
using System;
class Program
{
static void Main()
{
int age = 25;
Console.WriteLine("Age: " + age);
}
}
Long Data type
The long
data type in C# is used to store large integer values.
The long
data type is used to store large whole numbers (integers) that are too big for the int
type. It takes 8 bytes (64 bits) of memory and can hold values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- ✅ You need to store very large numbers.
✅int
is not enough (e.g., population count, financial transactions, etc.). - ✅
long
is for very large whole numbers.
✅ It takes more memory thanint
, so use it only when needed.
✅ Always useL
suffix for very large numbers to avoid errors.
Example
using System;
class Program
{
static void Main()
{
long distance = 5000000000;
Console.WriteLine("Distance in meters: " + distance);
}
}
Float Data Type
A float
is used to store numbers with decimals, but not as precise as double.
A float
is a number that can have a decimal point. But there’s a catch—it’s not super precise! It holds about 7 digits before it starts rounding things off.
- Size: 4 bytes (small but powerful!)
- Example values:
3.14
,99.99
,0.0005
- Precision: Up to 7 digits
Imagine you’re measuring water in a glass. You might say “2.5 cups” instead of just “2 cups” because you need that little extra detail. That’s exactly what float
does in C#—it helps store numbers with decimal places!
✅
Usefloat
when you need decimals but don’t need crazy precision.✅
Always addf
at the end (3.14f
,5.99f
) to tell C# it’s a float.✅
Not good for money! It might round off cents. Usedecimal
instead for prices.✅
Takes less memory thandouble
, so it’s great for speed and memory-saving.
float
is like a quick estimate—good for speed but not for super-precise numbers. If you’re tracking pizza slices or running speeds, it works fine. But if you’re calculating bank balances, you might want something more accurate!
Example
using System;
class Program
{
static void Main()
{
float pi = 3.14f;
Console.WriteLine("Approximate value of Pi: " + pi);
}
}
Double Data Type
Thedouble
data type is used to store double-precision floating-point numbers for greater accuracy thanfloat
.
Alright, Imagine you’re a pilot flying a plane, and you need to track the speed very accurately. Airplane speed is usually measured in kilometers per hour (km/h), and small changes matter a lot!
Since double
is more precise than float
, it’s perfect for storing speed values.
- Size: 8 bytes (twice as much as
float
) - Example values:
3.1415926535
,99.9999999
,0.00000012345
- Precision: 15-16 digits (way better than
float
!)
A double
is just like a float
, but it’s more accurate. It can store decimal numbers with up to 15-16 digits without messing them up.
✅
Usedouble
when you need more precision (e.g., scientific calculations, distances, large numbers).✅
No need forf
at the end (unlikefloat
). Just write the number normally!✅
More memory (8 bytes), but more accurate thanfloat
.✅
Not ideal for money calculations (usedecimal
for that).
Think of double
as a better version of float
. If float
is like a notebook, double
is like a textbook—it stores more information without errors.
using System;
class Program
{
static void Main()
{
double pi = 3.14159265359;
Console.WriteLine("Precise value of Pi: " + pi);
}
}
Decimal Data Type
Best for money or high-precision calculations
Think you’re at a store buying groceries. You grab some apples, milk, and bread. The total comes out to $12.99. But wait—what if the system rounds off the price and charges you $13.00 instead? That’s not fair, right?
That’s why we use the decimal
data type in C#! It’s super accurate and perfect for money-related calculations.
What is decimal
?
A decimal
is a number with a decimal point, just like float
and double
, but it’s way more precise. It keeps up to 28-29 digits without messing up.
- Size: 16 bytes (Big, but worth it!)
- Example values:
12.99
,12345.6789
,0.0000000001
- Precision: 28-29 digits (Better than
float
anddouble
!)
Why Use decimal
Instead of float
or double
?
- ✅ Super accurate → No rounding issues like
float
ordouble
.
✅ Perfect for money → Banks, stores, and accountants love it!
✅ Can handle large numbers → Great for big transactions.
Final Thought
If you’re dealing with money, prices, or anything that needs exact numbers, always use decimal
. It saves you from rounding errors and keeps calculations fair and accurate!
using System;
class Program
{
static void Main()
{
decimal price = 19.99m;
Console.WriteLine("Item Price: $" + price);
}
}
2. Boolean Type
The bool
type is used to store values that are either true or false. It’s very useful when you want to check something in your code.
✅ Stores only true
or false
✅ Uses bool
keyword
✅ Helps in decision-making
✅ Example values: true
, false
Booleans are useful for conditions, flags, and logic in programming!
Example
using System;
class Program
{
static void Main()
{
bool isRaining = true;
bool isSunny = false;
Console.WriteLine("Is it raining? " + isRaining);
Console.WriteLine("Is it sunny? " + isSunny);
}
}
3. Character Data Type
The char
type stores a single character, like a letter or symbol. It’s great for storing individual characters. It must be written inside single quotes ('
).
✅ Stores only one character
✅ Uses char
keyword
✅ Always written in single quotes ('A'
, 'B'
, '1'
, '@'
)
✅ Example values: 'X'
, '5'
, '#'
The char
type is useful for letters, symbols, and ASCII values in programming!
Example
using System;
class Program
{
static void Main()
{
char grade = 'A';
Console.WriteLine("Your grade is: " + grade);
}
}
Conclusion
Great job! Now you have a solid understanding of C# value types like numeric data types, bool
, and char
. You also got hands-on experience with examples—because, let’s be honest, coding makes more sense when you actually try it!
But wait, there’s more! Value types don’t stop here.
There are more value types in C# that we haven’t covered, like structs, enums, and nullable types. Don’t worry—we’ll get to them later in this tutorial. They’re just as important and will help you write even better C# code.
So, keep going! The best way to learn is to practice.