Visual C# Guide
Data Types

When we want to use a variable in a C# program, we must first declare the variable.

When programming, it is often necessary to store information so that it can be used at a later time in a program. In low level languages, the programmer specifies the exact location (in memory or in a particular register of the processor) where the information is stored. In high level languages, we use variables to represent stored information.

Imagine a series of boxes, each one with a label on the front to name the box. These names are called variables in C#. The contents of each box is the information stored in the variable.

It is useful to give variables meaningful names that can be easily understood by the programmer (you).

There are rules about variable names in C#.

Variable Names

The name that you give to a variable should help you to remember what the variable was to be used for. In C#, it is usual to use camelCase when writing variable names. A variable name must be a single word with no spaces. It should start with a lower case letter. If two or more words are combined together into a single variable name, we captialize the first letter of each word after the first. For example,

numberOfEntries = 5;
wholeNumber1 = 7;


The following words are used in the C# language and cannot be used as variable names.


Built-In Data Types

The following lines are examples of variable declarations in C#. The word, int specifies that the integer data type will be used. You have a choice about whether to initialize the variable in its declaration statement.

int myWholeNumber;
int anotherWholeNumber = 5;
Data TypeDescriptionMemory AllocatedRange
intSigned 32-bit integer4 bytes-2147483648 to 2147483647
byteUnsigned 8-bit integer1 byte0 to 255
longSigned 64-bit integer8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
doubleDouble-precision floating-point number8 bytesPrecise to about 15-16 digits
decimalSuitable for monetary calculations16 bytesPrecise to 28-29 significant digits
charStores a single Unicode character.2 bytes 
stringStrings are enclosed in double quotes.As required 
boolBoolean value (either true or false)  

Declaring Constants

A constant is a programmer-specified value that does not change throughout the lifetime of the program. Think of it as a variable that you cannot change. To declare a constant in C#, you do the following,

const double pi = 3.141593;

Notice that you use the keyword, const followed by the data type.

Local & Global Variables - Scope

The scope of a variable determines which parts of the program can access its values. The variables in the program on the next page are all local variables. That means that they can only be used within the block of code where they are declared.

Global variables are declared for use throughout the program. We will meet more of these later. It is not always desirable to use global variables in complex programs since it can be easy to change the value by accident.