C# - PRIMITIVE DATATYPES
📘 1. What are Primitive Data Types in C#?
✅ What
Primitive data types are basic built-in types used to store simple values like numbers, characters, and true/false.
👉 Examples:
-
int,double,char,bool
✅ Why use Primitive Data Types?
- ✔ Fast and efficient
- ✔ Directly supported by CPU
- ✔ Less memory usage
- ✔ Easy to use
❌ Why NOT (Limitations)?
- ❌ Cannot store complex data
- ❌ Fixed size (limited range)
- ❌ No advanced behavior like objects
✅ How to Use?
// Syntax
datatype variableName = value;
📊 2. Common Primitive Data Types in C#
| Data Type | Size | Example | Description |
|---|---|---|---|
| int | 4 bytes | 10 | Whole numbers |
| float | 4 bytes | 10.5f | Decimal numbers |
| double | 8 bytes | 10.55 | Large decimals |
| char | 2 bytes | 'A' | Single character |
| bool | 1 bit | true | True/False |
| byte | 1 byte | 255 | Small numbers |
💡 3. Example Program with Comments
using System;
namespace PrimitiveDemo
{
class Program
{
static void Main(string[] args)
{
// Integer type (stores whole numbers)
int age = 25;
// Double type (stores decimal values)
double salary = 25000.75;
// Character type (stores single character)
char grade = 'A';
// Boolean type (true/false)
bool isActive = true;
// Float type (decimal with 'f')
float temperature = 36.5f;
// Display values
Console.WriteLine("Age: " + age);
Console.WriteLine("Salary: " + salary);
Console.WriteLine("Grade: " + grade);
Console.WriteLine("Active: " + isActive);
Console.WriteLine("Temperature: " + temperature);
Console.ReadKey();
}
}
}
📦 4. Memory Storage (STACK vs HEAP)
🔹 What is Stack?
- Stores primitive data types
- Fast access
- Fixed size
- LIFO (Last In First Out)
🔹 What is Heap?
- Stores objects and reference types
- Dynamic memory
- Managed by Garbage Collector
⚙️ 5. How Primitive Data is Stored
👉 Primitive types (like int, bool, char) are stored in:
✅ STACK memory
🧠 Example
int x = 10;
int y = x;
📊 Stack Diagram
STACK MEMORY
-----------------
| y = 10 |
| x = 10 |
-----------------
👉 Explanation:
-
xstores 10 -
ygets a copy of 10 - Both are independent
🔁 Value Type Behavior (Important)
int x = 10;
int y = x;
y = 20;
📊 Stack Now
STACK MEMORY
-----------------
| y = 20 |
| x = 10 |
-----------------
👉 Changing y does NOT affect x
📦 6. Heap Example (For Understanding Difference)
class Person
{
public string name;
}
Person p1 = new Person();
p1.name = "John";
Person p2 = p1;
📊 Memory Diagram
STACK HEAP
------ ----------------
p1 ----┐ | name = John |
├----------> | Person Obj |
p2 ----┘ ----------------
👉 Both p1 and p2 point to same object
🔥 Key Difference
| Feature | Primitive (Value Type) | Object (Reference Type) |
|---|---|---|
| Memory | Stack | Heap |
| Storage | Direct value | Reference (address) |
| Copy | Value copy | Reference copy |
| Speed | Fast | Slower |
🎯 Real-Time Example
👉 Employee age vs Employee object
int age = 30; // stored in stack
Employee emp = new Employee(); // object in heap
🧠 Summary Diagram
VALUE TYPE (int, bool)
----------------------
STACK → stores actual value
REFERENCE TYPE (class, object)
------------------------------
STACK → stores reference
HEAP → stores actual object
✅Tips
- Primitive types = Value types
- Stored in Stack
- Faster and memory efficient
- Copied by value (not reference)
- 👉 C# primitive data types are value types stored in stack memory, which hold actual values directly and provide fast and efficient data handling.
Comments
Post a Comment