INTRODUCTION TO PROGRAMMING & BASIC SYNTAX IN C#

4. Variables and Data Types

What are Variables?

Variables are named storage locations in memory that hold data which can be changed during program execution.

Common Data Types in C#

Data Type

Description

Example

Memory

int

Integer numbers

int age = 25;

4 bytes

double

Decimal numbers

double price = 19.99;

8 bytes

float

Decimal numbers (smaller)

float temperature = 98.6f;

4 bytes

char

Single character

char grade = 'A';

2 bytes

string

Text of any length

string name = "John";

varies

bool

True/False values

bool isActive = true;

1 bit

 

 Variable Declaration and Initialization

// Declaration only

int number;

// Declaration with initialization

string name = "Habiba";

double salary = 45000.50;

// Multiple variables of same type

int x = 5, y = 10, z = 15;

// Using var for implicit typing

var message = "Hello World"; // compiler infers string type

Constants
Variables that cannot change during program execution:

const double PI = 3.14159;

const int MAX_SCORE = 100;