A variables is basically a storage in a computer’s memory where you can put a value that will be used by your program. Picture it as a container that holds your data. The contents of the container can be removed or changed anytime. The variables has a name, which identifies it among other variables and allows you to access the value of the variable. It has an address which determines where in the memory it resides. It has a value which is either assigned by the user or a result of a calculation. A variables can have a value of null or nothing. A variable has a data type, which signifies what kind of data the variables contains. And finally, a variable has a scope which tells you where in a program the variable is available for use.

We use variables as temporary storage of data or values. When creating programs, we need means for storing results, values, or input data from the user. This is the role of the variable. The variable got its name from the term variability which means changing or don’t have a consistent state. This is because we can assign and change the value of a variable whenever it is necessary. Variables are temporary and will only be usable when the program is running. When you close your program, values stored in variables are erased.

The name of a variable is called an identifier. The variable’s content is accessed or modified using its identifier. When naming your variable, your identifier should abide by the following rules:

  • An identifier should start with an alphabet (a-z or A-Z)
  • An identifier cannot contain special characters such as #, ?, ^, $.
  • An identifier cannot be any of the reserved keywords of C#.
  • An identifier cannot contain spaces.

Identifiers are also case-sensitive. C# is a case-sensitive language, which means, a and A are two different characters. Two variables named myNumber and MyNumber are two different variables because the first one starts with ‘m’ and the other one starts with ‘M’. You cannot declare a variable which is exactly the same as the name of another variable within the same scope. Scope means the block of code a variable is available for use. You will learn more about scope in a later lesson.

A variable has a data type which is the format or kind of data that it is expected to hold. Data type is used to allow a computer to describe what kind of data it holds and what operations you can do to it. Some of the most common data types are intdoublestringcharfloat, and decimal. Each one is tailored for storing a certain type of data. For example, if you are expecting a variable to hold an integer, you will give it a type of int. If you are expecting a variable to hold string literals or text, you would want to give it a data type of string. More complex data types can be made in C# by making your own classes or structures which are a topic of later lessons.

So now that you know the concept of variables, let’s look at how we can use them in C#.