Saturday, August 17, 2013

C# - Types of Variables

In C# coding language we have multiple types of variables that we can use to store values in memory, this article will provide a basic review about the main variables that are available, including code examples.

Overview
A Variable is a cell in the computer memory, this cell can contain different data types of information. When defining a Variable in our code, it’s must be unique (The variable name) in our program because the application should always know the specific cell to use.

In addition, there are different types of variables (Old/Primitive types and Objects/Instances of classes) that are managed differently in the computer memory allocation (value type and Reference types).

For Example:

Int – Host range of numbers(Primitive Type).
Bool – can receive True/False (Primitive Type).
Object - Determined via the class configuration (Reference Type)  

Where can you define Variables..?
  • You can declare variables inside a class.
  • You can declare variables in a function.
  • You can declare variables as part of a function parameters.
  • You can declare a variable in a specific type of loops.

Variable 1 - INT:
The ‘int’ variable is a cell in memory with a capacity 32 bits.
Min number -2147483648
Max Number: 214483647

class Program
    {
        static void Main(string[] args)
        {

           //declaring and initialization on the same row

           int firstnumber = 4;

           //declaring variable and initialize on different row

    int secondnumber;

           secondnumber = 5;

           //declaring multiple variables in the same row
   
    int x = 10 , y = 12 , z = 13;

    //initialize variable with other variable value

           int x = 10;

    int y = x;
        }
    }
Variable 2 - DOUBLE:
The ‘double’ variable is a cell in memory with capacity 64 bits, and not like the ‘Int’ variable, double can store decimal values.

Min number:  -1.79769313486232E+308
Max Number: 1.79769313486232E+308
class Program
    {
        static void Main(string[] args)
        {
            double A = 0.33333;
            double B = 3.4;      
        }
    }

Variable 3 - CHAR:
The char variable can store any value that is available  on your keyboard

class Program
    {
        static void Main(string[] args)
        {
            char a = '%';
            char b = 'F';
            char c = '3';
        }
    }

Variable 4 - STRING:
The String Variable can host every syntax you desire, you can add 10K words or single char.

class Program
    {
        static void Main(string[] args)
        {
            string a = "1";
            string b = "*";
            string c = "All.................................";
        }
    }
Variable 4 - Boolean:
The Boolean Variable can receive only two values (true/false)

Size in memory: 8 bits
class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = false;
        }
    }

No comments:

Post a Comment

My Presentations