Computing - C# Variables

Declaring Variables

Variables are used when holding data in memory while a program runs. So the program can set aside the right amount of memory to hold our information, we declare our variables to be of a particular data type.

Declaring Variables Video [3.1MB Quicktime .mov 01:39]


// Declare a variable called number1 to hold a whole number
int number1;

// Declare a variable called firstName to hold some text
string firstName;

// Declare a variable called eventDate to hold a date
DateTime eventDate;

Note: Comments have been included in the code examples to help explain certain parts of the code. Comments start with // and are there to help us understand the program but are ignored by the computer.

There is a number of different data types that we will come across on a regular basis:

int
Integers (whole numbers). E.g. 5, -123, 5600204
double
Numbers with decimal parts. E.g. 6.23, -0.0054, 2.0
string
Text. E.g. "Hello World!"
bool
Boolean. True or False
DateTime
Dates, Times, Dates and Times. E.g. 16/10/2009

Variable declaration
A variable called number1 is created to hold a whole number.

Assignment

To use a variable, we assign it a value using the assignment operator, =. The value from the right of the assignment operator is stored in the variable named on the left of the operator.


// Declare a variable called number1 to hold a whole number
int number1;

// Assign a value of 6 to the variable called number1
number1 = 6;

// Declare a variable called firstName to hold some text
string firstName;

// Assign a value of "Barry" to the variable called firstName
firstName = "Barry";

The value assigned can be the result of a calculation.


// Declare three variables to hold whole numbers
int number1;
int number2;
int total;

// Assign values to number1 and number2
number1 = 6;
number2 = 21;

// Find the sum of number1 and number2 and store it in total
total = number1 + number2;

As our programs get bigger, having readable, descriptive names for our variables will make our programs easier to follow and understand.

Variable assignment
The literal value 6 is placed in the variable called number1.

Try It Out

Create a new Console Application and try out the following program.


class Program
{
    static void Main(string[] args)
    {
        // Declare three variables to hold whole numbers
        int number1;
        int number2;
        int total;
        
        // Assign values to number1 and number2
        number1 = 6;
        number2 = 21;

        // Find the sum of number1 and number2 and store it in total
        total = number1 + number2;
        
        // Display the values of the variables
        Console.WriteLine("The value of number1 is {0}", number1);
        Console.WriteLine("Tha value of number2 is {0}", number2);
        Console.WriteLine("The sum of number1 and number2 is {0}", total);
    }
}

The value of number1 is 6
The value of number2 is 21
The sum of number1 and number2 is 27

Assigning Values to Variables Video - Using the assignment operator with literal values and expressions. [5.1MB Quicktime .mov 02:23]

Shortcuts

Multiple variables of the same data type can be declared together in a list.


int num1, num2, num3;
string text1, text2;

Variables can be declared and assigned an initial value in a single statement.


int num1 = 17;
string text1 = "Hello";
DateTime today = DateTime.Today;

Variable declaration and assignment
The expression is evaluated and its value placed in the variable called total.

Variables MindMap
Click image to enlarge
View as PDF

Test Yourself

Test yourself on these topics with the Question Generator.

DofE Training Nat Theatre DofE Training DofE Training DofE Training Nat Theatre