1. What Are Variables? (Simple Definition)
A variable is like a container in memory that stores data.
Think of it like:
C# Definition
A variable in C# is a named storage location in memory used to store a value.
It has:
Name
Data Type
Value
Syntax
dataType variableName = value;
Example
int age = 25;
string name = "Sandhiya";
double price = 99.50;
Real-Time Example (Easy Explanation)
Imagine you are building a shopping website:
| Variable Name | Data Type | Purpose |
|---|
| productName | string | Store the product name |
| productPrice | double | Store the price |
| stockCount | int | How many items available |
string productName = "Laptop";
double productPrice = 59999.99;
int stockCount = 10;
2. What Are Data Types?
A data type tells the compiler what type of data a variable will store.
C# Data Types Categories
1. Value Types (store data directly)
2. Reference Types (store memory address)
string
arrays
class objects
Common Data Types with Examples
Below are all commonly used data types with simple, real-time examples.
1. int (Integer)
Stores whole numbers (no decimal).
int age = 25;
int stock = 100;
Real-time: Number of items in cart, number of login attempts.
2. double (Decimal Number – high precision)
double salary = 20000.75;
double rating = 4.5;
Real-time: Price, ratings.
3. float (decimal but less precision)
float temperature = 32.5f;
Has ‘f’ at end.
4. decimal (Money values – very accurate)
decimal accountBalance = 10500.75m;
Used for banking, finance.
5. char (Single character)
char grade = 'A';
6. string (Sequence of characters)
string customerName = "Sandhiya";
string email = "[email protected]";
Real-time: Names, messages, emails, addresses.
7. bool (true/false)
bool isVerified = true;
bool isLoggedIn = false;
Real-time: Login success, item isActive, user subscription.
Real-Time Mini Example (All Combined)
Scenario: Online product page
string productName = "Mobile Phone";
int stock = 50;
double price = 14999.99;
bool isAvailable = true;
char category = 'A';
Code Example: Small Console App
using System;
class Program
{
static void Main()
{
string name = "Sandhiya";
int age = 24;
double height = 5.4;
bool isStudent = true;
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Height: " + height);
Console.WriteLine("Student? " + isStudent);
}
}
Summary Table
| Data Type | Example | Purpose |
|---|
| int | 20 | Whole numbers |
| double | 20.50 | Decimal values |
| decimal | 100.50m | Money |
| string | "Hello" | Text |
| char | 'A' | Single character |
| bool | true/false | Logical values |