To declare a variable in C#, you write the type, followed by the name, followed by = and the initial value:
string choice = "1";string— the type of value the variable will holdchoice— the name you use to refer to this variable"1"— the initial value assigned to it
Type Inference with var#
Instead of writing the type explicitly, you can use var and let the compiler figure it out:
var choice = "1";The type is inferred from the value on the right. This works as long as you assign a value when declaring the variable.
Common Types#
| Type | Holds | Example |
|---|---|---|
string | Text | "Hello" |
int | Whole numbers | 42 |
double | Decimal numbers | 3.14 |
bool | True or false | true |
Naming Rules#
- Names can contain letters, digits, and underscores.
- Names cannot start with a digit.
- Names are case-sensitive:
choiceandChoiceare different variables. - Use clear, descriptive names that reflect the value being stored.