Variables

A program that always displays the same message can get a bit boring. Most programs get some information, do some calculations, and then display the results of the calculations.

Information is stored in memory as variables. Variables can vary, or change. Before a variable can be used, it must be declared. To declare a variable, first decide what type of information you want to store:

These are the simple types in C#:

  • Signed integral: sbyte, short, int, long
  • Unsigned integral: byte, ushort, uint, ulong
  • Unicode characters: char
  • IEEE floating point: float, double
  • High-precision decimal: decimal
  • Boolean: bool

For the purposes of this course, the following will be used:

  • Whole numbers, number with no decimal places are called int (integer).
  • Numbers with decimal places are called double.
  • Single letters such as 'Y' or 'N'' are char (characters).
  • A variable that can only have the values true and false is bool (Boolean).
  • Words, such as "Hello", or a person's name are strings.

In addition to the types listed above you will also sometimes declare variables to be one of the special types built-in to C#:

 Color myFavColor = Color.PeachPuff;

NEXT: Variable Types