C Sharp for Beginners/Variables

Variables are simply places to store data like numbers, strings (text), arrays of numbers or strings, and other objects. Each variable can only store data of one type, and you must declare variables before attempting to set or get their values. Here is a sample program that uses variables:

The output would be: 100 Hello! 200

You can see that variables are declared using the form  and are set using the   operator. You can declare multiple variables of the same type by placing commas after each variable name:

If you want to set variables immediately after you declare them, you can use the form :

The output would be exactly the same as before.

Types
C# offers various types of variables besides  and. Here is a short list of the available types:


 * - stores true or false.
 * - stores an unsigned byte.
 * - stores a signed byte.
 * - stores a single character:
 * - stores an integer:
 * ,  - both store an integer.
 * ,,   - both store an unsigned integer
 * - stores a floating-point number:
 * - stores a double-precision floating-point number.
 * - stores a quadruple-precision floating-point number.
 * - stores a string, or a sequence of characters.

Note: a floating-point number is a number which can be fractional. For example, 3.14159 is a floating-point number while 314 is just an integer.

You may be wondering why there are so many types for storing integers and floating-point numbers. Signed integers can be negative as well as positive, while unsigned integers can only be positive. So,,  ,   and   can only store positive integers.

There are also different sizes for integers and floating-point numbers; a type with a bigger size has a bigger range than a type with a smaller size. Here are the sizes of the types:


 * - 8 bits, or 1 byte.  can store numbers from 0 to 255, while   can store numbers from -128 to 127.
 * - 16 bits, or 2 bytes.  can store numbers from -32,768 to 32,767, while   can store numbers from 0 to 65,535.
 * - 32 bits, or 4 bytes.  can store numbers from negative 2 billion to positive 2 billion, while   store numbers from 0 to 4 billion.
 * - 64 bits, or 8 bytes.  can store numbers from negative 9 million million million to positive 9 million million million, while   can store numbers from 0 to 18 million million million.

You may also be wondering why there are small types when we could use the biggest available types. The answer is that most computers today are 32-bit, which means that they are designed to handle 32-bit numbers. 64-bit numbers are therefore slower to add, subtract, multiply, and divide.