Learn Programming

Learn C# Programming Tutorial Lesson 2 - Variables and Constants

What is a variable?

A variable is a block in memory that you can use in a program to store data. You can store different types of data such as numbers and words.

Using variables

Before you can use a variable you must declare it. To declare a variable you must first choose what data type it is. If you want to store a number then you must use the int data type. You must then choose a name for the variable. We will use the name MyInt in the following example.

int MyInt;

To store a value in a variable you must use an =. Here is an example of how to store the value 5 inside an integer variable.

MyInt = 5;

You can store a value inside a variable when you declare it. This is called initializing the variable.

int MyInt = 5;

To declare 2 or more variables at once just separate them with a comma.

int i,j;

You can perform calculations on variables using a +, -, *(multiply), or /(divide). When you perform a calculation you must put the variable that is going to store the value on the left of the = and the calculation on the right. Here are some examples.

int a,b,c,d;
a = 1 + 2;
b = 2 - 1;
c = 5 * 3;
d = 10 / 5;

The double data type is used for storing numbers that include a decimal point.

double MyDouble = 1.2;

The char data type stores single letters and special characters. You need to put a char inside single quotes when using them. This is because numbers can also be characters but when a number is a character then it is not a number anymore.

char MyChar = 'c';

A string is a data type that can store many characters together. You must put strings in double quotes when using them. You can also join 2 strings together using a +.

string MyString = "This";
string s = MyString + " is a string";

The bool data type can only store either a true or a false.

bool MyBool = true;
MyBool = false;

Type conversions

You can't just store a char type in an integer variable. You have to perform a type conversion if you want to do something like this. If you are converting between numbers like an integer and a double then all you need to do is put the type you want to convert to in brackets in front of the value that must be converted.

double d = 7.5;
int i = (int)d;

If you want to convert between types like a string and a number then you need to use special conversion functions. There is a special class called Convert that has methods for converting between data types.

int i = 5;
string s = Convert.ToString(i);
double d = Convert.ToDouble(i);

Constants

Constants are values that can't have their value changed. By making a certain number a constant you are giving it a name which means more than the value by itself. You have to set the value of a constant when you declare it.

const double PI = 3.14;

Input and output

The Console.ReadLine command is used for reading values from the keyboard and putting them into a variable. Console.WriteLine prints the value of a variable. Here is an example program that asks the user to enter something and then stores the entered number in a variable and then prints it out again.

Console.Write("Enter something: ");
string s = Console.ReadLine();
Console.WriteLine(s);