Week 1 - Variables and Data Types
Learning Goals
- Declare and initialise variables in C#.
- Use the correct data type for different kinds of information.
- Display variable values to the user.
Key Concepts
A variable is a named storage location in memory. You must tell C# what type of data it will hold.
| Data Type | Used For | Example Value |
|---|---|---|
string |
Text or words | "Apple" |
int |
Whole numbers | 42 |
double |
Decimal numbers | 3.99 |
bool |
True or false | true |
DateTime |
Dates and times | DateTime.Now |
Declaring and Initialising a Variable
Displaying Values
You can also use string interpolation:
Your Task
Create a new Console App in Visual Studio called Week1_Variables.
Write a program that:
- Declares variables for a single shop item:
- item name (
string) - price (
double) - quantity in stock (
int) - whether it is on special (
bool) - Displays all four values neatly to the console.
Expected Output
Extension
- Add a
DateTimevariable calleddateAddedand set it to today's date usingDateTime.Today. - Display it in your output.
Common Mistakes to Avoid
- forgetting the semicolon
;at the end of each line - using
intfor a price instead ofdouble - putting numbers inside quotes when you want numeric data