C# Variables

A variable is a storage location that holds some kind of value.

x = 5

In C# you must declare the variable data type. Make a new C# Console Project and name it Variables.

Syntax

<data type> <name> = <value>;

or

<data type> <name>;

C# is a strongly typed language, meaning you need to specify the data type such as integer, string, Boolean, or float. If you don’t know the data type you can use the 'var' keyword.

Example

int X = 5;

In this example we have a variable called X which is an integer data type, and gave it the value 5.

int A;

In this example we have a variable called X which is an integer  data type and gave it the value 5.

The common data types are:

Variable Type Full Description
int Integer - whole numbers
string Set of characters
bool Boolean value (true of false)
float Floating point number
double Floating point numbers, but more accurate
char Single characters

Variable Rules:

  • Variables must start with a number, letter or @ symbol.
  • Do not create variables with the same name but different cases; for example myName and myname are valid but they can become very confusing.

Now, make a new string variable called name and give it your name, and copy this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Variables
{  

    class Program
    {
        static void Main(string[] args)
        {


            string name = "Asim"; // Assign the variable name to Asim - the variable is a string
            Console.WriteLine(name); // Print out the variable 
            Console.ReadLine(); // stop and wait for a key to be pressed. 
        }

    }

}

Code Explained:

  • First you made a new string variable called 'name' and assigned the value of your name
  • Then you used the Console.WriteLine command to print out 'name' - which was the value of your name
  • The last line tells the compiler to stop and wait for a key to be pressed; if you do not include the last line then the program will open and close quickly

When you make a string variable you must put the value in quotation marks ( "" ). Make sure that your casing is correct, otherwise it will not work.

It can also be written as:

string name;
name = Asim;

Both are accepable.

This time we will repeat the same process but we will print out an integer value. Comment out the other code and then type:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Variables
{  

    class Program
    {
        static void Main(string[] args)
        {

            //string name = "Asim"; // Assign the variable name to Asim - the variable is a string
            //Console.WriteLine(name); // Print out the variable 
            //Console.ReadLine(); // stop and wait for a key to be pressed. 

            int x = 5;
            Console.WriteLine(x);
            Console.ReadLine();

        }

    }

}

Code Explained:

  • You created a integer variable and gave it the name 'x' and value 5
  • You then printed it out using the Console.WriteLine command

Notice how this time we did not need to use quotation marks, because it is a integer variable and it does not need quotation marks.

You can also add, subtract, multiply and divide integers. In the previous code under the x variable, create a new variable called 'a' and give it the value 10.

Then, print it out using the Console.WriteLine command but this time write (x + a):

int x = 5;
int a = 10;

Console.WriteLine(x + a);
Console.ReadLine();

Variables can be changed; you can change the variable x to 20 and you will get the result 30. Variables are useful for storing and retrieving data. Variables in a class are called fields. For the above example, you should get the result 15.

Summary

  • Variables must start with a number, letter or @ symbol
  • Do not use variables with the same name but different casing
  • Variables made in a class are called fields