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:

class VariablesExample
{
    public static void Main()
    {
        int number;
        string myText;
 
        number = 100;
        System.Console.WriteLine(number);
        myText = "Hello!";
        System.Console.WriteLine(myText);
        number = 200;
        System.Console.WriteLine(number);
        System.Console.ReadKey();
    }
}

The output would be:

100
Hello!
200

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

int x, y, z;

If you want to set variables immediately after you declare them, you can use the form [type] [variable name] = [value];:

class VariablesExample2
{
    public static void Main()
    {
        int number = 100;
        string myText = "Hello!";
 
        System.Console.WriteLine(number);
        System.Console.WriteLine(myText);
        number = 200;
        System.Console.WriteLine(number);
        System.Console.ReadKey();
    }
}

The output would be exactly the same as before.

TypesEdit

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

  • bool - stores true or false.
  • byte - stores an unsigned byte.
  • sbyte - stores a signed byte.
  • char - stores a single character:
    char theLetterA = 'a';
    
  • int - stores an integer:
    int number = 100;
    
  • short, long - both store an integer.
  • ushort, uint, ulong - both store an unsigned integer
  • float - stores a floating-point number:
    float number = 3.14159;
    
  • double - stores a double-precision floating-point number.
  • decimal - stores a quadruple-precision floating-point number.
  • string - 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, byte, ushort, uint and ulong 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:

  • byte, sbyte - 8 bits, or 1 byte. byte can store numbers from 0 to 255, while sbyte can store numbers from -128 to 127.
  • short, ushort - 16 bits, or 2 bytes. short can store numbers from -32,768 to 32,767, while ushort can store numbers from 0 to 65,535.
  • int, uint - 32 bits, or 4 bytes. int can store numbers from negative 2 billion to positive 2 billion, while uint store numbers from 0 to 4 billion.
  • long, ulong - 64 bits, or 8 bytes. long can store numbers from negative 9 million million million to positive 9 million million million, while ulong 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 (long, decimal). 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.

Last modified on 14 June 2009, at 19:18