Variables are containers used to store data in a program. In C#, each variable must be declared with a specific data type that defines the kind of data it can hold.
The basic syntax to declare a variable is:
data_type variableName;
Example:
int age; string name; double price;
You can assign values to variables when declaring them or later in the code:
int age = 25; string name = "Alice"; double price = 99.99;
Or assign values after declaration:
age = 30; name = "Bob"; price = 49.95;
Data Type | Description | Example |
---|---|---|
int | Stores whole numbers (integers) | int score = 100; |
double | Stores decimal numbers (floating-point) | double price = 12.99; |
string | Stores text | string name = "John"; |
bool | Stores true/false values | bool isActive = true; |
age
, price
, or userName
make your code easier to read.userName
or UserName
).Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!