Menu
Coddy logo textTech

Numbers

Part of the Fundamentals section of Coddy's C# journey — lesson 5 of 69.

Variables are containers that hold data values. They are used to store, manipulate, and display information within a program.

In short, a variable is like a memory unit that we can access by typing the name of the variable.

Each variable has a unique name and a value that can be of different types. C# has various built-in data types that define the type of value a variable can hold.

To initialize a variable, we use the following format:

variableType variableName = value;

In C#, numbers are typically represented using two main data types: int and double.

int is used to store whole numbers without any decimal point. For example:

int age = 30;
int temperature = -5;
int count = 100;

double is used to store numbers with a decimal point. For example:

double price = 99.99;
double pi = 3.14159;
double fraction = 0.5;

When declaring variables in C#, you need to specify the type of the variable before the variable name. This is known as type declaration. Once a variable is declared with a certain type, it can only hold values of that type.

challenge icon

Challenge

Beginner

Write a C# program that declares and initializes the following variables:

  • Declare an int variable named quantity and initialize it with the value 5.
  • Declare a double variable named itemPrice and initialize it with the value 24.99.

Don't forget the semicolon in the end of each line!

Cheat sheet

Variables are containers that hold data values. To initialize a variable, use the format:

variableType variableName = value;

int is used for whole numbers:

int age = 30;
int temperature = -5;

double is used for numbers with decimal points:

double price = 99.99;
double pi = 3.14159;

In C#, you must specify the variable type before the variable name (type declaration). Once declared, a variable can only hold values of that type.

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        // Declare and initialize variables here
        
        
        // Don't change below
        Console.WriteLine("Quantity: " + quantity);
        Console.WriteLine("Price: " + itemPrice);
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals