Variables are an important part of computer programming. Essentially, a variable is any value saved in a computer's RAM to be used later. Whenever you use a variable, you first need to declare it so that the computer sets aside space for it later, then you need to initialize it by giving it a value. Here are the four most common variable types found in C#:
Boolean Variables are variables that take a true or false value. You can use these for asking yes or no questions, or anything else in which there are only two answers. If you were to declare and then initialize a boolean variable, your code might look something like this:
Boolean b;
b = false;
As you can see, declaring a variable and assigning values can be very easy. From this point onwards, if you copied down that code, your program would recognize that whenever you reference the variable "b" it would have the value "false."
Integers are variables that deal with positive and negative whole numbers. If a decimal number gets converted to an integer, it will not round. Instead it will simply be chopped off before the first decimal place. To declare and initialize an integer, this would be your code:
int32 a;
a = 6:
Doubles are the variable to use when you are dealing with decimal numbers. Technically, the variables that use decimal numbers are called "floating point" variables; double is just short for "double precision floating-point number." To use a double in a program, modify the following code:
Double g;
g = 6.42
Strings are variables that deal with words. Strings are very useful because they allow you to quote things that the user inputs. Strings can use numbers, words and punctuation, but you can't do math with strings. The following is code for declaring and initializing a string variable:
string answer;
answer = "Abraham Lincoln";
One important note: you are able to declare and initialize variables in the same line if you wish, and when the program you are making allows. If you were to declare and initialize a double in one line it would look like this:
Double a = 54.39;