C#  

Variables and Data Types in C#

1. Variables (Definition + Explanation + Examples)

What is a Variable?

A variable is a container that stores data in memory while the program is running.

Think of a variable like a box with a label:

  • The label = variable name

  • What you put inside the box = value

  • The type of box = data type

Example

int age = 25;
string name = "Sandhiya";

Here:

  • age is a variable that stores an integer.

  • name stores a string (text).

Key Points About Variables

  1. Must have a data type

    • Example: int number;

  2. Must have a name

    • Example: string fullName;

  3. Can store a value

    • Example: double price = 10.5;

  4. Value can change

    • Example

      int count = 1;
      count = 5;
      

Real-Time Example (Easy to Understand)

Imagine booking a movie ticket website.

  • Your Age → int variable

  • Your Name → string variable

  • Ticket cost → double variable

  • Is the payment successful? → bool variable

Example

string customerName = "Sandhiya";
int customerAge = 23;
double ticketPrice = 150.50;
bool isPaymentDone = true;

2. DATA TYPES (Definition + Uses + Examples)

What is a Data Type?

A data type tells C# what kind of value a variable can store.

Example

int age = 22;

int tells the computer:

"This variable will store whole numbers."

C# Data Types Fully Explained

A. Primitive (Basic) Data Types

1. int (Integer)

Stores whole numbers (no decimals)

int salary = 30000;

2. double (Decimal numbers)

Stores numbers with decimals

double rating = 4.5;

3. float (Decimal, smaller precision)

Ends with f

float temperature = 36.6f;

4. decimal (Money values — highest accuracy)

Ends with m

decimal productPrice = 999.75m;

Used for payment apps, billing systems, e-commerce
(because money requires accuracy)

5. char (Single character)

Must be single quotes ' '

char grade = 'A';

6. bool (True/False)

Used for conditions

bool isLoginSuccess = false;

7. string (Text)

Stores multiple characters.

string city = "Chennai";

B. Non-Primitive Types

8. Arrays

Stores multiple values of same type

int[] marks = { 80, 90, 95 };

9. List

Dynamic collection (very common in C# & .NET Core)

List<string> names = new List<string>();
names.Add("Sandhiya");
names.Add("Priya");

Real-Time Example: Food Delivery App Data Types

InformationExample ValueC# Data Type
Customer Name"Sandhiya"string
Customer Age23int
Order Price450.75double
Order Statustrue/falsebool
OTP Code'5'char
List of Items["Burger", "Pizza"]List

C# Program Example: Variables + Data Types

using System;

class Program
{
    static void Main(string[] args)
    {
        string customerName = "Sandhiya";
        int quantity = 2;
        double price = 150.50;
        bool isDelivered = false;

        double total = quantity * price;

        Console.WriteLine("Customer: " + customerName);
        Console.WriteLine("Quantity: " + quantity);
        Console.WriteLine("Total Price: " + total);
        Console.WriteLine("Order Delivered: " + isDelivered);
    }
}