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 TypeSizeExampleDescription
int4 bytes10Whole numbers
float4 bytes10.5fDecimal numbers
double8 bytes10.55Large decimals
char2 bytes'A'Single character
bool1 bittrueTrue/False
byte1 byte255Small 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:

  • x stores 10
  • y gets 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

FeaturePrimitive (Value Type)Object (Reference Type)
MemoryStackHeap
StorageDirect valueReference (address)
CopyValue copyReference copy
SpeedFastSlower

🎯 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

Popular posts from this blog

Interview Tips: Dot NET Framework vs Net CORE

OOP Concept in real-time scenario: Mobile Device Management Software

FREE Webinar: Run Your Own Independent DeepSeek LLM