C Sharp Data Types

C# Data Types – Complete Beginner Guide
Understanding C# data types is one of the most important foundations of programming.
Every variable in C# must have a data type. The data type tells the compiler:
What kind of value the variable will store
How much memory it needs
What operations can be performed on it
If you choose the wrong data type, your program may give errors or unexpected results.
In this complete beginner guide, you’ll learn:
What data types are in C#
Why data types are important
Value types vs Reference types
Built-in data types
Numeric data types
Boolean, char, and string
var keyword
Nullable types
Type conversion
Best practices
Common beginner mistakes
Let’s get started
What Are Data Types in C#?
A data type defines the type of data a variable can store.
Example:
1 | int age = 25; |
Here:
intis the data typeageis the variable25is the value
The data type ensures that only whole numbers can be stored in age.
Why Are Data Types Important?
Data types:
- Define memory size
- Improve performance
- Prevent errors
- Enable correct operations
- Improve code clarity
For example:
1 2 | int number = 10; number = "Hello"; // Error |
You cannot assign text to an integer variable.
Categories of Data Types in C#
C# data types are mainly divided into:
- Value Types
- Reference Types
Value Types
Value types store the actual data directly in memory.
Examples:
int
double
float
char
bool
struct
enum
Example:
1 | int number = 100; |
The value 100 is directly stored in memory.
Reference Types
Reference types store the address of the value.
Examples:
string
object
class
array
Example:
1 | string name = "Sanjit"; |
The variable stores a reference to the actual string in memory.
Built-in C# Data Types
C# provides several built-in data types.
Numeric Data Types
Used to store numbers.
Integer Types
| Type | Size | Range |
|---|---|---|
| byte | 1 byte | 0 to 255 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | ±2 billion |
| long | 8 bytes | Very large numbers |
Example:
1 2 | int age = 25; long population = 8000000000; |
Floating-Point Types
Used for decimal numbers.
| Type | Precision |
|---|---|
| float | 6-7 digits |
| double | 15-16 digits |
| decimal | High precision (money) |
Example:
1 2 3 | float temperature = 36.6f; double price = 199.99; decimal salary = 15000.75m; |
Note:
float requires
fdecimal requires
m
Boolean Data Type (bool)
Stores only two values:
true
false
Example:
1 | bool isActive = true; |
Used in:
Conditions
Loops
Comparisons
Character Data Type (char)
Stores a single character.
Example:
1 | char grade = 'A'; |
Must use single quotes.
String Data Type (string)
Stores text (sequence of characters).
Example:
1 | string name = "Sanjit"; |
Strings use double quotes.
Strings are reference types.
The object Data Type
The object type can store any data type.
Example:
1 2 | object value = 10; value = "Hello"; |
However, use carefully to avoid confusion.
The var Keyword (Implicit Typing)
C# allows automatic type detection.
Example:
1 2 | var age = 25; var name = "Sanjit"; |
The compiler automatically determines:
age → int
name → string
Important:
Must assign value immediately
Type cannot change later
Nullable Data Types
Normally, value types cannot store null.
Example:
1 | int age = null; // Error |
To allow null:
1 | int? age = null; |
Useful when value is optional.
Default Values of Data Types
If declared at class level:
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| bool | false |
| char | ‘\0’ |
| string | null |
Local variables must be initialized before use.
Type Conversion in C#
Sometimes you need to convert one data type to another.
Implicit Conversion (Automatic)
Smaller → Larger type
1 2 | int number = 10; double value = number; |
No data loss.
Explicit Conversion (Casting)
Larger → Smaller type
1 2 | double price = 99.99; int newPrice = (int)price; |
Decimal part lost.
Using Convert Method
1 2 | string input = "25"; int age = Convert.ToInt32(input); |
Useful when taking user input.
Real Beginner Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; class Program { static void Main() { int age = 20; double height = 5.9; char grade = 'A'; string name = "Sanjit"; bool isStudent = true; Console.WriteLine($"Name: {name}"); Console.WriteLine($"Age: {age}"); Console.WriteLine($"Height: {height}"); Console.WriteLine($"Grade: {grade}"); Console.WriteLine($"Student: {isStudent}"); } } |
This demonstrates multiple data types working together.
Choosing the Right Data Type
Choose based on:
- Type of data
- Size of value
- Precision required
- Performance needs
For example:
Use int for whole numbers
Use double for general decimals
Use decimal for money
Use bool for conditions
Common Beginner Mistakes
- Using wrong data type
1 | int price = 99.99; // Error |
- Forgetting suffix
1 | float number = 10.5; // Error |
Correct:
1 | float number = 10.5f; |
- Not initializing variable
- Confusing string and char
1 | char letter = "A"; // Error |
Correct:
1 | char letter = 'A'; |
Best Practices for Data Types
- Use most appropriate type
- Avoid using object unnecessarily
- Use decimal for financial values
- Use var when type is obvious
- Avoid unnecessary type casting
Value Types vs Reference Types (Quick Comparison)
| Feature | Value Type | Reference Type |
|---|---|---|
| Stored in | Stack | Heap |
| Stores | Actual value | Memory reference |
| Examples | int, double | string, class |
Understanding this helps in advanced programming.
Why Mastering Data Types Is Important
Data types are the backbone of programming.
They allow you to:
Store information
Perform calculations
Build conditions
Create applications
Avoid runtime errors
Every C# program depends heavily on data types.
Frequently Asked Questions (FAQs)
1. What are data types in C#?
Data types define the type of value a variable can store.
2. What is the difference between value and reference types?
Value types store actual data, while reference types store memory references.
3. What is var in C#?
var allows the compiler to automatically determine the data type.
4. Which data type should I use for money?
Use decimal for financial calculations.
5. Can int store decimal values?
No. int only stores whole numbers.
Final Thoughts
C# data types are:
Essential
Foundational
Powerful
Beginner-friendly
Mastering data types allows you to:
Write accurate programs
Prevent errors
Improve performance
Move toward advanced C# concepts
Understanding data types clearly makes the rest of C# much easier.
