Skip to content

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

string productName = "Apple";
int quantity = 10;
double price = 1.99;
bool inStock = true;

Displaying Values

Console.WriteLine("Product: " + productName);
Console.WriteLine("Price: $" + price);

You can also use string interpolation:

Console.WriteLine($"Product: {productName}");
Console.WriteLine($"Price: ${price}");

Your Task

Create a new Console App in Visual Studio called Week1_Variables.

Write a program that:

  1. Declares variables for a single shop item:
  2. item name (string)
  3. price (double)
  4. quantity in stock (int)
  5. whether it is on special (bool)
  6. Displays all four values neatly to the console.

Expected Output

--- Shop Item ---
Name:       Chocolate Bar
Price:      $2.50
Stock:      24
On Special: True

Extension

  • Add a DateTime variable called dateAdded and set it to today's date using DateTime.Today.
  • Display it in your output.

Common Mistakes to Avoid

  • forgetting the semicolon ; at the end of each line
  • using int for a price instead of double
  • putting numbers inside quotes when you want numeric data