C# Variables
C# Variables
Variables in C# are used to store data that can be used and changed during program execution.
What is a Variable?
A variable is a named memory location that holds a value.
-
int→ data type -
age→ variable name -
25→ value
Declaring Variables
Or declare and assign together:
Common Data Types
| Data Type | Description | Example |
|---|---|---|
int |
Integer numbers | int x = 5; |
double |
Decimal numbers | double pi = 3.14; |
float |
Decimal (less precision) | float temp = 36.5f; |
char |
Single character | char grade = 'A'; |
string |
Text | string name = "Vipul"; |
bool |
True/False | bool isActive = true; |
Multiple Variable Declaration
Constants
A constant value cannot be changed.
Variable Naming Rules
✔ Must start with a letter or _
✔ Cannot start with a number
✔ Cannot use C# keywords
✔ Case-sensitive
Type Inference (var)
The compiler automatically detects the type.
⚠ var variables must be initialized.
Changing Variable Values
Output Variables
Summary
✔ Variables store data
✔ Must have a data type
✔ Values can be changed
✔ const is used for fixed values
✔ var simplifies declarations
