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

ORACLE 11g: User Authentication System with Stored Procedure and SQL*Plus Integration